3

The following code is working. It's expected to return the app last launch date, but...

// e.g. entryPath = @"/Applications/Evernote.app"

struct stat output;
int ret = stat([entryPath fileSystemRepresentation], &output);
if (ret == 0)
{
    struct timespec accessTime = output.st_atimespec;
    NSDate * date1 = [[NSDate dateWithTimeIntervalSince1970:accessTime.tv_sec]
            dateWithCalendarFormat:nil timeZone:[NSTimeZone localTimeZone]];

    // and the 10.6+ version: (the result was always the same for me)
    NSDate * date2 = [[[[NSURL fileURLWithPath:entryPath]
            resourceValuesForKeys:[NSArray arrayWithObject:NSURLContentAccessDateKey]
                            error:nil]
            objectForKey:NSURLContentAccessDateKey]
            dateWithCalendarFormat:nil timeZone:[NSTimeZone localTimeZone]];
}

It returns very strange dates, usually near 3:00-3:30 AM, today or yesterday, when I was actually sleeping. But after that, I click "Get Info" in Finder, and it shows the real last launch date (which is usually less than date1 and date2, but can be also equal). How to get it programmatically? Finder get info: Last opened date

Dmitry Isaev
  • 3,888
  • 2
  • 37
  • 49
  • Looks like this comes from Spotlight, though I'm not sure how to obtain the value programmatically. The key is `kMDItemLastUsedDate`. – paulmelnikow Dec 17 '12 at 19:40
  • @noa: You'd use `MDItemCreateWithURL`. Feel free to make that an answer. – Peter Hosey Dec 18 '12 at 05:47
  • @noa: In your answer, you may use [the sample code](http://stackoverflow.com/a/6726872/1289683). :) Thanks so much! Spotlight features are awesome! P.S. Found [Spotlight Metadata Attributes](https://developer.apple.com/library/mac/#documentation/Carbon/Reference/MetadataAttributesRef/Reference/CommonAttrs.html#//apple_ref/doc/uid/TP40001694-SW1) explanation. – Dmitry Isaev Dec 18 '12 at 06:32

0 Answers0