Get Last Opened date of the file

Robert. I was under the impression that URL resource keys had to be preceded by current application’s, although my script works without it. Perhaps its only required when getting multiple resource keys. Thanks for the correction.

use framework "Foundation"
use scripting additions

set theFile to (choose file)
set theURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of theFile)
-- the following line works
set theKeys to {current application's NSURLContentAccessDateKey, current application's NSURLContentModificationDateKey}
-- the following line doesn't work
set theKeys to {NSURLContentAccessDateKey, NSURLContentModificationDateKey}
set {theResult, theError} to theURL's resourceValuesForKeys:theKeys |error|:(reference)

Normally I would agree with you.
But I noticed in ‘Script Debugger’ that that line

set theKey to current application’s NSURLContentAccessDateKey

returns an NSString of the same name as the key.
which is how I determined I could just just send the string directly

The script in post 23 that I said didn’t work contained an error. The corrected version is:

use framework "Foundation"
use scripting additions

set theFile to (choose file)
set theURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of theFile)
set theKeys to {"NSURLContentAccessDateKey", "NSURLContentModificationDateKey"}
set {theDates, theError} to theURL's resourceValuesForKeys:theKeys |error|:(reference)

The following is from page 64 of Shane’s ASObjC book. So, my use of “current application’s NSURLContentAccessDateKey” would seem an acceptable approach.

set {theResult, theValue} to theURL's getResourceValue:(reference) forKey:(current application's NSURLContentModificationDateKey) |error|:(missing value)

I compared the two approaches with random files of varying age and there are differences. The kMDItemLastUsedDate approach appears to accurately reflect what is shown in a Finder window and may be preferred for that reason.

Out of curiosity, how do these variances compare with shell’s stat -f '%a' or stat -f '%m'? Those stand for ‘access time’ and ‘modified time’, respectively.† I’m not sure what underlying mechanisms mdls uses to determine its time stamps and I generally find them unpleasant to deal with.

The above commands will return epochal seconds (e.g. 1681870300) but they can be converted to other formats, like so;

date -r 1681870300 -R
--> Tue, 18 Apr 2023 22:11:40 -0400

date -r 1681790875 "+%Y-%m-%d %H:%M:%S"
--> 2023-04-18 00:07:55

Obviously, the latter result combined with applescript’s date command will generate a date object.

† Excerpted from man stat:

The time-related fields of struct stat are as follows:

st_atime Time when file data last accessed.  
    Changed by the mknod(2), utimes(2) and read(2) system calls.

st_mtime Time when file data last modified.  
    Changed by the mknod(2), utimes(2) and write(2) system calls.

It seems that the atime is affected by actions such as quicklook, and occasionally just by combining the above into a single command.

set tFile to (choose file)
set qpFile to quoted form of POSIX path of tFile
do shell script "acco=$(stat -f '%a' " & qpFile & ") ; date -r $acco \"+%Y-%m-%d %H:%M:%S\" "
--> "2023-04-18 23:25:57"

Another good argument to use the approach of @Fredrik71 and the NSURLContentAccessKey I’m wondering why Apple hasn’t unified the data.

I ran some quick tests to compare the file-opened-dates as returned by the three approaches contained in this thread. These approaches were KniazidisR’s NSMetadataItem script, my implementation of Fredrik71’s NSURLContentAccessDateKey script, and Mockman’s stat utility script. My test files were a text file I created today and Shane’s ASObjC book (a PDF). I opened these files using the method indicated, and I simply opened and then immediately closed the documents. The returned file-opened-dates were the same except when the file was opened with Quick Look.

I ran some timing tests, and Mockman’s script took 7 milliseconds, and the other scripts took 1 millisecond (with the foundation framework in memory).

Just as an aside, I randomly tested the scripts with some older files on my boot drive, and Kniazidisr’s script often returned missing value. The other scripts did return a date. I don’t know the reason for this, except that the metadata may have been missing for some reason.

TEST FILE ONE - a text file created today
FILE OPENED WITH - KNIAZIDISR - PEAVINE - MOCKMAN
TextEdit - 5:49.38 - 5:49.38 - 5:49.38
Finder (double-click) - 5:52:37 - 5:52:37 - 5:52:37
Quick Look - 5:52:37 - 5:54:39 - 5:54:39
NSWorkspace’s openURL - 6:02:03 - 06:02:03 - 06:02:03

TEST FILE TWO - Shane’s ASObjC book (a PDF)
FILE OPENED WITH - KNIAZIDISR - PEAVINE - MOCKMAN
Preview - 6:14:35 - 6:14:35 - 6:14:35
Finder (double-click) - 6:16:23 - 6:16:23 - 6:16:23
Quick Look - 6:16:23 - 6:17:42 - 6:17:42
NSWorkspace’s openURL - 6:19:14 - 6:19:14 - 6:19:14

As an afterthought, I repeated the above tests with a script that I created with Script Debugger, and the results were not the same as above.

TEST FILE THREE - a script last modified 5:38 today
FILE OPENED WITH - KNIAZIDISR - PEAVINE - MOCKMAN
Script Debugger - 7:10:11 - 7:07:10 - 7:07:10
Finder (double-click) - 7:23:17 - 7:07:10 - 7:07:10

2 Likes