How to programmatically get the "last opened" date like displayed in Finder
Asked Answered
B

2

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.

Barramunda answered 3/6, 2013 at 14:15 Comment(3)
Does mdls (on Terminal) show an entry for kMDItemLastUsedDate? Also, note that you are leaking attributes and lastUsedCfDate.Tomboy
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.Barramunda
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.Tomboy
L
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.

Labyrinthodont answered 12/5, 2014 at 16:7 Comment(2)
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...Barramunda
@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.Labyrinthodont
K
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.

Kato answered 22/4, 2016 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.