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.
mdls
(on Terminal) show an entry forkMDItemLastUsedDate
? Also, note that you are leakingattributes
andlastUsedCfDate
. – Tomboymdls
and realized that thekMDItemLastUsedDate
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 thekMDItemLastUsedDate
? I ask, because I want to copy files and set all attributes to the values of the source file. – Barramundaxattr
(both the command-line utility and the API) unless you write a Spotlight importer. How are you copying the file? Have you triedcopyfile(3)
?cp -p
copies some attributes but notkMDItemLastUsedDate
. – Tomboy