Script to Find Latest Added File to Folder

I’ve searched here as well as a general Google search and found nothing (although there is a thread here that seems to be along the same lines as what I’m trying to do, but none of the scripts contained in this post work for me).

I need to select a folder (I can change the path later as it is always the very same folder) and find the last added file to that folder, and copy that file to the desktop. Simple, or so I thought. Thanks in advance for any help!

Homer712. I don’t know of a way to get a file’s date-added property with basic AppleScript. You can get it from a file’s metadata, but that doesn’t work if the file is on a drive that’s not indexed, which leaves ASObjC.

I’ve included my suggestion below, which works as expected on my Sonoma computer. A few comments:

  • The script throws an error if the file exists on the desktop or if the source folder is empty.

  • The script skips package files but that can be changed.

  • The timing result with a small folder was 67 milliseconds, and 64 milliseconds of that was attributable to the Finder duplicate command. I don’t believe System Events supports the duplicate command, so the alternatives are the shell or ASObjC.

use framework "Foundation"
use scripting additions

set sourceFolder to POSIX path of (choose folder)
set targetFolder to (path to desktop)
set theFiles to getFiles(sourceFolder)
set newFile to POSIX file (item 1 of theFiles) -- will error if no files found
tell application "Finder" to duplicate newFile to targetFolder -- will error if file exists

on getFiles(theFolder)
	set fileManager to current application's NSFileManager's defaultManager()
	set theFolder to current application's |NSURL|'s fileURLWithPath:theFolder
	set fileKey to current application's NSURLIsRegularFileKey
	set dateKey to current application's NSURLAddedToDirectoryDateKey
	set pathKey to current application's NSURLPathKey
	set folderContents to fileManager's contentsOfDirectoryAtURL:theFolder includingPropertiesForKeys:{} options:4 |error|:(missing value) -- skips hidden files
	set theFiles to current application's NSMutableArray's new()
	repeat with anItem in folderContents
		set {theResult, aRegularFile} to (anItem's getResourceValue:(reference) forKey:fileKey |error|:(missing value))
		if aRegularFile as boolean is true then (theFiles's addObject:(anItem's resourceValuesForKeys:{dateKey, pathKey} |error|:(missing value)))
	end repeat
	theFiles's sortUsingDescriptors:{current application's NSSortDescriptor's sortDescriptorWithKey:dateKey ascending:false}
	return (theFiles's valueForKey:pathKey) as list -- returns a list of POSIX paths
end getFiles
1 Like

Your script works perfectly, finds the correct file (I have BetterZip add a date to the file after it’s encrypted, FileName.xlsx_2023-12-27.zip) and copies it to the desktop. Now I can add an Automator app to run BetterZip, unzip the file and then move it to the proper location. Many thanks!

OK, I’ve run into an issue and it may not be solvable. Happened this morning when I ended up with a file with today’s date, rather the a date from a few days ago. So, the question is this, is it possible in either Apple Script or Automator to use “wildcards” to identify a file?

This script worked . . .

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Finder"
	set theFile to "File Name.xlsx_2023-12-27.zip"
	set theName to "File Name.zip"
	set the name of file theFile to theName & ".zip"
end tell

so long as the file had the “2023-12-27.zip” in the name, but will not work with today’s file, which is named “2023-12-31.zip”

I’ve done quite a bit of searching, but nothing seems to work. Ive tried *.zip, ?.zip and ~.zip with no luck at all.

Is it even possible?

Homer712. When changing the name of a file with Finder, you have to specify its path. Otherwise, Finder is unable to find the file. I’m surprised that your script worked previously, so perhaps I’m missing something.

The following worked on my computer:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Finder"
	set theFile to "Macintosh HD:Users:Robert:Desktop:File Name.xlsx_2023-12-31.zip"
	set theName to "File Name.zip"
	set the name of file theFile to (theName)
end tell

BTW, the above script also worked if the source file is named 2023-12-31.zip, but I changed the path in the script to reflect this.

Thanks for working with me on this. Thirty seconds after posting it hit me that because the file name will change on a pretty regular basis (two to three times a week) I’d be much better off inserting one of those “select file” lines in the script that’s meant to change the file name. Here’s what I came up with:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Finder"
	set sourceFile to POSIX path of (choose file)
	set targetFile to (path to desktop)
	set theName to "File Name.zip"
end tell

It does everything correctly, but actually change the name of the file on the desktop. In the Script Debugger “result” panel it shows the file (attached screenshot) but I can’t get it to actually change the file name on the desktop.

The following script works as expected. There’s no need to specify a target file, because sourceFile is being renamed in place–not moved or copied.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set sourceFile to (choose file)
tell application "Finder"
	set name of sourceFile to "File Name.zip"
end tell

BTW, if you want to copy a file to the desktop and then rename it:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set sourceFile to (choose file)
set targetFolder to (path to desktop)

tell application "Finder"
	set copiedFile to (duplicate sourceFile to targetFolder)
	set name of copiedFile to "File Name.zip"
end tell

Thank you, working perfectly. I now have an Automator application that goes out, gets the dated zip file from an iCloud folder and puts it on the Desktop, then deletes the date part of the file name, extracts the file with Better Zip and moves it to the required location. And it all happens in the blink of an eye!

Many thanks to you and others who have responded to my questions here. I have a folder in Documents named “MacScripter Stuff” where I keep all the original scripts that have been provided here. When I come up with something new that I’d like to accomplish, my first step is to look through that folder and see if I can piece together a few scripts to do what I’m looking to do. The scripts you provided here in this post have been added to that folder resource . . . thank you!

1 Like

Fredrik71, you give me way too much credit for what I currently understand about AppleScript. Even with your explanations, most of what you posted went over my head. But, I do very much appreciate the scripts and information you have provided. I have saved this entire post as a PDF file and have added it to my “MacScripter Stuff” folder for when I have a better understanding of AppleScript. Thank you!