Get "Date Added" in AppleScript

In Lion, folders can now be sorted by “Date Added,” in addition to modification and creation dates. Unfortunately, this isn’t accessible through file properties, but only through Spotlight metadata. I came up with a little sub-routine to access this datum, and thought I’d share it with the community. It uses a bit of meta-scripting in order to coerce a date, so if anyone knows a way around this, please chime in!

on getDateAdded(theFile)
	set aFilePath to POSIX path of theFile
	do shell script "mdls -name kMDItemDateAdded -raw " & (quoted form of aFilePath)
	do shell script "ruby -e \"require 'date'\" -e \"puts DateTime.parse('" & result & "').strftime('%m-%d-%Y')\""
	run script ("get date \"" & result & "\"")
	return result
end getDateAdded

nik;

This is great – I’m going to move it from here to Code Exchange after it’s been here for long enough to gather comments.

Without the ruby, it’s this:


set DA to do shell script "mdls -name kMDItemDateAdded -raw " & quoted form of POSIX path of (choose file)
set delim to "/"
tell DA to set dateAdded to word 3 & delim & word 2 & delim & word 1

returns a date coercible string


tell current application to (do shell script ("mdls -name kMDItemDateAdded -raw " & (quoted form of (POSIX path of (choose file))) & " | xargs -0 -I indate date -j -f '%Y-%m-%d %H:%M:%S' indate +'%x %r'"))

Hello

At this time, only the ruby one is using worldWide.
The two other return the date in US format : mm/dd/yy hh:mm:ss

Would be fine to return the ISO format :

yyyy-mm-ddThh.mm.ss

I edited the second script to return the local format :


set DA to do shell script "mdls -name kMDItemDateAdded -raw " & quoted form of POSIX path of (choose file)
set dateAdded to current date
set day of dateAdded to 1
set year of dateAdded to word 1 of DA
set month of dateAdded to word 2 of DA
set day of dateAdded to word 3 of DA
set hours of dateAdded to word 4 of DA
set minutes of dateAdded to word 5 of DA
set seconds of dateAdded to word 6 of DA
dateAdded

Yvan KOENIG (VALLAURIS, France) dimanche 26 février 2012 12:41:10

Hello

I ignore everything about ruby but the script returns a wrong value, at least on a French system.

Here is the log report which I get :

tell current application
do shell script “mdls -name kMDItemDateAdded -raw ‘/Important/archivage Kbase/kBase.nmbtemplate/’”
→ “2012-02-25 11:06:15 +0000”
do shell script “ruby -e "require ‘date’" -e "puts DateTime.parse(‘2012-02-25 11:06:15 +0000’).strftime(‘%m-%d-%Y’)"”
→ “02-25-2012”
run script “get date "02-25-2012"”
→ date “dimanche 26 février 2012 02:25:00”
end tell
Résultat :
date “dimanche 26 février 2012 02:25:00”
The correct value is samedi 25 février 2012 11:06:15

Yvan KOENIG (VALLAURIS, France) dimanche 26 février 2012 15:01:42

On my Lion machine the time of day is always wrong (compared to the Finder). The Finder pays attention to your local time zone, while mdls lists the time as GMT. In my case, that’s a “4 hour difference. mdls says 19:43 for a file I just saved, and the Finder (on a 12-hour clock) says 3:43, e.g., 15:43 in 24-hour clock time. If a time is near midnight, that results in a wrong date.

Hello Adam

What you get is the UTC (GMT) offset.

What I posted is worse.
Look at this example :

tell application “AppleScript Editor”
choose file
→ alias “Macintosh HD:Users:yvankoenig:Desktop:specs AppleWorks.cwk.zip”
end tell
tell current application
do shell script “mdls -name kMDItemDateAdded -raw ‘/Users/yvankoenig/Desktop/specs AppleWorks.cwk.zip’”
→ “2011-11-29 14:43:00 +0000”
do shell script “ruby -e "require ‘date’" -e "puts DateTime.parse(‘2011-11-29 14:43:00 +0000’).strftime(‘%m-%d-%Y’)"”
→ “11-29-2011”
run script “get date "11-29-2011"”
→ date “lundi 27 février 2012 11:29:00”
end tell
Résultat :
date “lundi 27 février 2012 11:29:00”

The initial shell script returns the correct date time.
It’s the Ruby code which is wrong.
It extract the date in the US format → 11-29-2011 so, when the last instruction : get date. apply, on a non English system it return a foolish value.


set dateUS to "11-29-2011"
log (get date dateUS)
(*date lundi 27 février 2012 11:29:00*)
set dateFR to "29-11-2011"
log (get date dateFR)
(*date mardi 29 novembre 2011 00:00:00*)

I know no alternate way than the one which I posted to build a conversion valid everywhere.

Some times ago, Nigel Garvey posted a piece of code taking care of the UTC offset.

Yvan KOENIG (VALLAURIS, France) lundi 27 février 2012 10:46:57

That’s hard to do, because you need the offset at the time and date in question, not now. Anyway, here’s an ASObjC solution that should work anywhere and handles the time zone difference:

script dateStringToDate
	-- get passed string as NSString
	set dateString to current application's NSApp's convertedValue()
	-- turn it back into the NSDate it was stored as
	set theNSDate to current application's NSDate's dateWithString_(dateString)
	-- convert it to an AS date using ObjectWithFords method ford_
	set theFord to current application's ObjectWithFords's alloc()'s init()
	set theASDate to theFord's ford_(theNSDate)
end script

set thePath to quoted form of "/Users/shane/Downloads/CSVData-9.csv"
set dateString to do shell script "mdls -name kMDItemDateAdded -raw " & (thePath)

tell application "ASObjC Runner"
	set theDate to (run the script {dateStringToDate} converting dateString with result returned)
end tell

Readers should note that Shane’s script requires that his ASObjC Runner be installed on your machine – it doesn’t come with the system.

Sorry, I should have made that clear. It’s also longer than it needs to be; ASObjC Runner does the conversion from NSDate to AS date automatically. So the script part only needs to be:

script dateStringToDate
	-- get passed string as NSString
	set dateString to current application's NSApp's convertedValue()
	-- turn it back into the NSDate it was stored as; it will be converted to an AS date on return
	set theNSDate to current application's NSDate's dateWithString_(dateString)
end script

Hello Shane

It seems that I missed some changes.

The script posted on 2012-02-27 05:11:32 am refuse to compile under 10.8.5 with ASObjC Runner version 1.9.13

It stop highlighting the word returned in the instruction :
set theDate to (run the script {dateStringToDate} converting dateString with result returned)

KOENIG Yvan (VALLAURIS, France) samedi 14 septembre 2013 09:21:31

Yvan,

Yes, that script was written with a very early version, and there was a conflict with a scripting addition’s terminology, so I changed it. Try this:

	set theDate to (run the script {dateStringToDate} converting dateString with response)

Hello.

It is possible to figure out the offset to UTC by using the scripts find in the thread Transposing dates between Time Zones by Nigel Garvey.

It is of course much more cumbersome than using AsObjC-Runner, but it is an alternative.

You should also be able to get the Time to UTC from the Spotlight Metadata with mdls -raw at least.

I get to be more and more impressed with AsObjC-Runner. :slight_smile:

Thanks Shane.
I assume that I forgot to edit the handler in my library when you introduced the change.
I will do that after posting this response.

KOENIG Yvan (VALLAURIS, France) samedi 14 septembre 2013 14:57:24