As learned from google and sister stack sites, this is the working solution I've come up with for getting the path of latest added file in a folder. But this is not giving the accurate result. I'm using Mavericks (OS 10.9)

set myFolder to "/Users/lawsome/folder"
tell application "Finder" to set latestFile to the last item of (sort files of (POSIX file myFolder as alias) by creation date) as alias
            set latestpath to POSIX path of latestFile
set latestpath to POSIX path of latestFile

Finally, Jose Alban's answer in here@askdiff gives an accurate sorted list using the mdls terminal command. How can I get the file path of last entry in this list.

Or any way to get the desired path.


The AppleScript code looks as thought it's getting the creation date rather than the date added. Finder and System Events don't store date added information in AppleScript. If you were running Yosemite, you could have done it with AppleScriptObjC. But, with Mavericks, the only avenue I can think of is by way of shell scripting.

Via bash script:

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)"

From within an AppleScript:

AppleScript has the ability to execute shell commands using do shell script. You can either run the whole command above using do shell script, which is probably what most people would do. But, just for demonstration purposes, I'm going to mix it with a bit of AppleScripting too.

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
  • I'm pretty sure UNIX doesn't store creation date, it only stores Last Modified Date. Everything else is added by Apple.
    – JBis
    Aug 13 '18 at 11:37
  • Sorry, I'll explain. You used kMDItemDateAdded which is not an entirely reliable way to check the date added. This is a feature added by Apple and is not in the real file system. I thought it should be explained why you had to use it: There's no other way to get the information because UNIX only stores Last Modified Date by default and not creation date or date added.
    – JBis
    Aug 13 '18 at 11:51
  • @JBis 👍🏼­­­ ­
    – CJK
    Aug 13 '18 at 12:02
  • simply amazed !!!
    – lawsome
    Aug 13 '18 at 16:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .