El código de AppleScript se ve como se cree que está obteniendo creation date
en lugar de date added
. Finder y System Events no almacenan información date added
en AppleScript. Si estuvieras ejecutando Yosemite, podrías haberlo hecho con AppleScriptObjC. Pero, con Mavericks, la única avenida en la que puedo pensar es a través de shell scripting.
A través de script bash:
F=~/folder \
&& [[ -d "$F" ]] \
&& mdls -name kMDItemFSName -name kMDItemDateAdded -raw "$F"/* \
| xargs -0 -I {} echo {} \
| paste -sd" \n" - - \
| sort \
| tail -1 \
| cut -f4- -d" " \
| printf '%s\n' "$F/$(cat)"
Desde dentro de un AppleScript:
AppleScript tiene la capacidad de ejecutar comandos de shell utilizando do shell script
. Puede ejecutar el comando completo arriba usando do shell script
, que es probablemente lo que haría la mayoría de la gente. Pero, solo para fines de demostración, también lo mezclaré con un poco de AppleScripting.
property text item delimiters : space
set myFolder to "/Users/lawsome/folder" -- Folder to be evaluated
-- Assemble shell command
set sh to the contents of {¬
"[[ -d", quoted form of myFolder, "]]", "&&", ¬
"mdls", "-name kMDItemFSName", "-name kMDItemDateAdded", ¬
"-raw", [quoted form of myFolder, "/*"], ¬
"|", "xargs", "-0 -I {} echo {}", ¬
"|", "paste", "-sd' \n' - -", ¬
"|", "sort"} as text
do shell script sh -- run the bash command
set latestFileAddedToFolder to the last paragraph of the result
--> e.g. 2018-08-13 08:02:52 +0000 Some File Name.txt
-- Split text up into date components and filename text
set [{yyyy, m, dd, HH, MM, SS, "+", timezone}, filename] to ¬
[words 1 thru 8, text 27 thru -1] of latestFileAddedToFolder
-- Construct AppleScript date object from date components
tell the (current date) to set ¬
[dateAdded, year, its month, day, time] to ¬
[it, yyyy, m, dd, hours * HH + minutes * MM + SS]
set pathToMostRecentlyAddedFile to myFolder & "/" & filename
return contents of {pathToMostRecentlyAddedFile, ¬
"was added to its containing folder", myFolder, ¬
"on", date string of dateAdded, ¬
"at", time string of dateAdded} as text