Setting Path to Download latest file

I have been trying everything I can think to find the latest download file. Assume it s simple error on my part.

In the script that follows I have set the path to the source folder and the option in the tell block does not work


set SourceFolder to "Mackintosh HD:Users:petermitchell:Dropbox:Investing:"

tell application "Finder"
	
	sort (get files of folder SourceFolder) by creation date
	set latestfile to (item1 of result) as alias
	
end tell

.

Thanks

Peter

just replace item1 with item 1

Thank you very much that was a dumb mistake, however it did not fix the problem. This is the message I now get

error “Finder got an error: Can’t get folder "Mackintosh HD:Users:petermitchell:Dropbox:Investing:".” number -1728 from folder “Mackintosh HD:Users:petermitchell:Dropbox:Investing:”

Unlike my original post I have checked very carefully to make sure the address is correct.

If I choose the folder rather than code it in it seems to work, seems because I am returning the oldest file rather than the newest so another issue.


choose folder with prompt "Get latest Folder"
set sourcefolder to result
tell application "Finder"
	
	sort (get files of folder sourcefolder) by creation date
	set latestfile to (item 1 of result) as alias
	
end tell
display dialog latestfile as string

so I am still doing something incorrectly thanks again.

This is the code if I choose the folder


choose folder with prompt "Find latest folder"
set sourcefolder to result

set sourcefolder to result
tell application "Finder"
	
	sort (get files of folder sourcefolder) by creation date
	set latestfile to (item 1 of result) as alias
	
end tell
display dialog latestfile as string

Peter

I ran your two scripts.
Both did the job flawlessly.

As choose folder returns an alias, the instruction supposed to sort the list of files may be shortened to:
sort (get files of folder sourcefolder) by creation date

In the second script the instruction
set sourcefolder to result
is repeated twice which is at least useless.

In your first message the pathname started with “Mackintosh HD” isn’t it “Macintosh HD” ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 4 décembre 2019 19:51:33

I just wanted to add that if your goal is to “find the latest download file,” then you don’t want to use “Creation Date” for the sort, because creation dates on files can be preserved when downloading them, depending on how the download occured. That is, it’s possible to download an old file and have it show up in the downloads folder with the old modification date, thus your script will return something else as the file with most recent “date created.”

Maybe you never download things such that their creation date is preserved, but I’d recommend using the Finder’s “Date Added” field, which should always return the most recent thing added to the folder.

Again what a dumb mistake. However following t.spoon comment I checked the dictionary to be sure and there is no “Date Added” for Finder. Don’t know why my original script always returned the first dated item. But when I combine the two instructions it works correctly.


set sourcefolder to "Macintosh HD:Users:petermitchell:dropbox:investing:"
set sourcefolder to result
tell application "Finder"
	set latestFile to last item of (sort (get files of folder sourcefolder) by creation date) as alias
end tell
display dialog latestFile as string

This now works thanks again.

“Date Added” was a relatively new addition to Finder, and as far as I know, Finder hadn’t gotten any Applescript updates since the date “Date Added” was added. Applescript doesn’t really know about Finder tabs either… Finder’s dictionary is just outdated.

You can access “date added” with Applescript using “do shell script” with mdfind, it’s the kMDItemDateAdded field. But if you’re getting the behavior you want, I wouldn’t worry about it.

I think Safari downloads show up with Date Added and Date Modified set to the time the download’s complete? So if that’s where all these files come from, it will probably work for you. My Downloads folder gets things from more sources, and some definitely preserve existing modification dates on the files.

  • t.spoon

With the usual proviso that the volume supports Spotlight searches and that the database is up-to-date.

You can also do it with my FileManagerLib:

use AppleScript version "2.5" -- macOS 10.11 or later
use scripting additions
use script "FileManagerLib" version "2.3.3"

date info for (choose file)
--> {added_date:date "Friday, 8 November 2019 at 7:03:53 pm", last_access_date:date "Friday, 8 November 2019 at 7:03:53 pm", creation_date:date "Wednesday, 31 October 2018 at 9:31:47 pm", modification_date:date "Wednesday, 31 October 2018 at 9:33:30 pm"}

Or to answer the original question:

use AppleScript version "2.5" -- macOS 10.11 or later
use scripting additions
use script "FileManagerLib" version "2.3.3"

set theFolder to (choose folder)
set theFiles to objects of theFolder
set latestFile to item -1 of (sort objects theFiles sorted property added property)