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

Hi All,
KM newbie here. I’ve made a super simple but really helpful macro that helps me automate exporting in Capture One Pro. It marks the image green, exports it, and moved to the next image. Now I’m trying to figure out a way to get KM to do the following which would allow me to selectively choose from exported images to add to a slideshow folder:

when I press a hotkey:

  1. Find most recently added file in EXPORT folder
  2. Make a copy of that file and paste it in SLIDESHOW folder

Any help would be much appreciated!

Dave

Since you are saying that the files are manipulated before they are exported, I assume that the most recently added file is always the most recently modified file.

If that is OK, this should do the trick:

[Test] Copy Most Recent File.kmmacros (2.2 KB)

Instructions:

  1. Set the path to your source and to your destination directory in the green actions.
  2. Assign a hotkey to the macro.


The shell script (red action) does this:

  1. It goes into the source directory.
  2. It lists the files by modification date and picks the first one.
  3. It copies that file to the destination directory.


Notes:

  • Files with identical names will be overwritten without prompt. (Behavior can be changed.)
  • There shouldn’t be any noise in the source directory (for example subfolders, files that don’t belong there, etc.).
2 Likes

Awesome! Thanks so much. Works great!

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