How to copy/paste last file added to folder only when hotkey is pressed?

Hey Dave,

Tom’s solution works perfectly if the files in question are modified in the order they are added to the source directory.

Over the years I’ve had semi-regular situations where I’ve modified a file in situ and created a false-positive, so I prefer to use the actual date-added date of the file whenever possible.

AppleScriptObjC gives us that ability since the advent of Mavericks (although Yosemite and later are easier).

Getting the Last-Added File in a Folder (Directory)

I’ve altered the main part of the script to perform your task:

------------------------------------------------------------------------------
use framework "Foundation"
use scripting additions
------------------------------------------------------------------------------

# User must specify source and destination directorys.
tell application "System Events"
   set sourceFolderPath to POSIX path of disk item "~/Downloads/"
   set targetFolderPath to POSIX path of disk item "~/test_directory/Destination_Directory/"
end tell

set theSortKey to (current application's NSURLAddedToDirectoryDateKey)
set lastAddedFile to its filesIn:sourceFolderPath sortedBy:theSortKey returningSingleItem:true
set lastAddedFile to alias POSIX file lastAddedFile
set targetFolderPath to alias POSIX file targetFolderPath

tell application "Finder"
   duplicate lastAddedFile to targetFolderPath without replacing
end tell

------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------

But remember – the handlers from the complete script are necessary to make it work.

The script is set to fail if a file with the same name already exists in the destination folder, although this is customizable of course.

-Chris

1 Like