7

I want to display the "Last opened" date in my App just like in the Finder preview or info panel. However I realized that this is NOT the same as the last access date that I would get with

NSDate* lastAccessDate = [fileUrl resourceValuesForKeys:@[NSURLContentAccessDateKey] error:NULL][NSURLContentAccessDateKey];

or with

struct stat buf;
stat(curName, &buf);
time_t lastAccessDate = buf.st_atimespec.tv_sec;

These return the the Unix last access time which is also displayed in Terminal for ls -l However the Finder displays a different value which only changes when the file is opened by the user (e.g. via double clicking)


I read the posts 'Get the real “last opened” date?' and '“Last Opened” Date' but these didn't solve it. They recommend something like

MDItemRef itemRef = MDItemCreateWithURL(NULL, (__bridge CFURLRef)fileUrl);
NSArray *attributeNames = (__bridge NSArray *)MDItemCopyAttributeNames(itemRef);
NSDictionary *attributes = (__bridge NSDictionary *) MDItemCopyAttributes(itemRef, (__bridge CFArrayRef) attributeNames);
CFDateRef lastUsedCfDate = MDItemCopyAttribute(itemRef, kMDItemLastUsedDate);
NSDate* lastUsedDate = (__bridge NSDate*) lastUsedCfDate;
CFRelease(itemRef);

But attributeNames does not have a value kMDItemLastUsedDate. In my case there are exactly 24 values in that array, but none for last use. So lastUsedDate is nil...

Also I wonder if there really is no high level API to access the last opened date.

Community
  • 1
  • 1
codingFriend1
  • 6,487
  • 6
  • 45
  • 67
  • 1
    Does `mdls` (on Terminal) show an entry for `kMDItemLastUsedDate`? Also, note that you are leaking `attributes` and `lastUsedCfDate`. –  Jun 03 '13 at 14:49
  • Ah, I played a bit with `mdls` and realized that the `kMDItemLastUsedDate` is not set for files that were copied but have not been opened, yet. However, the Finder does display a last opened time for these files, too. Can I manually set the `kMDItemLastUsedDate`? I ask, because I want to copy files and set all attributes to the values of the source file. – codingFriend1 Jun 03 '13 at 15:48
  • I’m not sure if you can set Spotlight attributes like you can with `xattr` (both the command-line utility and the API) unless you write a Spotlight importer. How are you copying the file? Have you tried `copyfile(3)`? `cp -p` copies some attributes but not `kMDItemLastUsedDate`. –  Jun 03 '13 at 16:06

2 Answers2

0

http://forums.macrumors.com/showthread.php?t=855913

To quote chown33:

Extended attributes are completely different from metadata. Xattrs are attached to the file, stored as part of the file-system. Metadata is extracted from the file, stored in the metadata store (essentially, Spotlight's database). Some xattrs are extracted and stored in the metadata store, but they're still two separate things.

The commands that operate on metadata fit the 'md*' globbing pattern: mdfind, mdls, mdutil, etc.

...

You generally don't get to update any of the metadata values. This is by design, from what I can see by looking at the low-level API.

Community
  • 1
  • 1
pw222
  • 815
  • 1
  • 6
  • 11
  • 1
    I'm sorry, but this does not answer the question. I know I can get the spotlight metadata using the md* commands. But I need to do this from Cocoa code and I would rather not create command line tasks and do console output parsing... – codingFriend1 May 13 '14 at 09:10
  • @codingFriend1, I was projecting my intentions on to you. I was trying to modify the metadata, but from what I found I think the answer to that is the metadata system was designed to disallow it. After re-reading your question, I see you're just trying to read the metadata. – pw222 May 14 '14 at 09:23
0

I ran into the very same problem. I found that when Finder encounters a case where the kMDItemLastUsedDate key is unavailable, it will show you the value for the kMDItemFSContentChangeDate key instead.

Pripyat
  • 2,937
  • 2
  • 35
  • 69