1

How could I get a file's "last opened" date ? I know how to get modification date but I didn't find anything on last used or last opened.

I want to arrange my files as in finder.

Thank you for any help ! (sandboxable would be much appreciated)

CSS - code
  • 135
  • 1
  • 8

2 Answers2

3

You need to look at the Spotlight metadata - the field you are interested in is kMDItemLastUsedDate: http://developer.apple.com/library/mac/#documentation/Carbon/Reference/MDItemRef/Reference/reference.html

Paul R
  • 208,748
  • 37
  • 389
  • 560
-1

If you mean "last accessed" date by "last opened" date, then there's a function in the C standard library for that:

#include <sys/stat.h>

struct stat st;
stat("somefile.ext", &st);
time_t accessed = st.st_atime;
  • I think this gives a different date/time than that shown in the Finder, which uses Spotlight's `kMDItemLastUsedDate` field apparently. – Paul R Dec 12 '12 at 17:05
  • @PaulR It may be the case, but why? Shouldn't they be the same? –  Dec 12 '12 at 17:06
  • I'm not sure that HFS+ actually stores atime, so it may just be that atime == mtime, hence the need to use Spotlight. Just guessing though... – Paul R Dec 12 '12 at 17:08
  • Thanks for your reply. Would last accessed not also imply it could be opened by the system, instead of the user. Also, would this function work in a sandboxed app in your judgment ? – CSS - code Dec 12 '12 at 17:40
  • @CSS-code IDK why would it not work in any app. Also, this does mean that it can be accessed by anything - are you not looking for that? You didn't specify you wanted to restrict this check to user interaction... –  Dec 12 '12 at 17:42
  • sry, it was mend to be restricted by the user. as I said like in finder, which also only shows you the files the user accessed. – CSS - code Dec 12 '12 at 18:40
  • mh.. is there also a c way to get if the last opener was the user ? – CSS - code Dec 12 '12 at 19:44
  • yes thats sure, but a way to get the type of opener, for example if the last opener was a system app or not – CSS - code Dec 12 '12 at 19:50
  • is st_atime support better on APFS (Apple's new file-system)? Is this API available (and not private) also on iOS? My goal is to find recently viewed Photos, when I have the photo-library access permission, and I can get to the file-URL of the locally stored image file. – Motti Shneor Jan 01 '20 at 10:41
  • Plus, is this Spotlight API also available for iOS? – Motti Shneor Jan 01 '20 at 10:43