Getting the URL of the File Most Recently Added to a Folder

Hey Dan,

-t gives you a sort by modified date.

Unfortunately ls does not provide a means to find the last-added-item.

-Chris

I told you I might be naive! Well, it was worth a shot.

Hi,

I am using this script it takes up to 7 seconds on our systems.Get latest file URL.kmactions (1.3 KB)

set sourceFolder to (path to documents folder)

tell application "Finder"
   
   set fileList to (sort (get files of sourceFolder) by creation date)
   
   -- This raises an error if the folder doesn't contain any files
   -- Last File is the Most Recently Created --
   set theFile to (last item of fileList) as alias
   
   set thePath to POSIX path of theFile
   set oProp to (properties of theFile)
   set URLstr to URL of oProp
end tell

set strCMD to "osascript -l JavaScript -e '(strPath = decodeURI(\"" & URLstr & "\").slice(7)).slice(strPath.indexOf(\"/\"))'"
set strPosixPath to (do shell script strCMD)

Is there a way to just get the most recent added file faster? all I want is the most recent added to the Folder. Preferably at an instant speed without the code checking all files to find the most recent added. Does OS X Sierra allow for a faster method than what I am currently using?

You can do an Execute Shell Script action with something like this:

ls "$KMVAR_sourceFolder" -Urp | grep -v / | sort -ur | head -1

with the output going to a variable. This will not include folders, but will also not include packages and applications.

1 Like

Could you provide this as a shell script please? Sorry i am not coding savvy.

Just add an "Execute a Shell Script" action, and paste the command into it. Set it to "save results to variable", and enter a variable name. Like this:

:slight_smile:

1 Like

Thank you for this. ?

Was that a statement or a question? :slight_smile:

Sorry i am writing from my iphone responding as an email with a smily face. I guess KM does not process them

1 Like

I now get this result :slight_smile:

/var/folders/ct/lsmxdhzn2hv4p378kqlxxf8c0000gn/T/Keyboard-Maestro-Script-744B68F0-8610-4B23-B9F1-57527CBB781E:0:4: script error: A “"” can’t go after this identifier. (-2740)

does not seem to work.

I am using:

ls “$KMVAR_sourceFolder” -Urp | grep -v / | sort -ur | head -1

I guess the question is, what is source folder?

“sourceFolder” is the name of the variable you showed in the original post. It should contain the path to the folder you want to examine.

1 Like

this will be on different imacs and the source folder has a different lcoation for each different iMac. But in all occassions we need to pull out the file from the Documents folder. This is always in the default place.

Every iMac has a different name so the initial location name would be different…

How can we overcome this problem?

Try this:

cd ~/Documents
ls -Urp | grep -v / | sort -ur | head -1
1 Like

Hi Thank you for this but I am still getting an error

/var/folders/ct/lsmxdhzn2hv4p378kqlxxf8c0000gn/T/Keyboard-Maestro-Script-A0085D98-F593-4D38-A947-0D56DD54DB11:0:4: script error: A unknown token can’t go after this identifier. (-2740)

You must have an invalid character in there somewhere. Try typing it by hand.

1 Like

FANTASTIC :), I was executing as applescript…

Would we be able to do the same for this code too?

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

use framework "Quartz"

-- classes, constants, and enums used
property nsurl : a reference to current application's nsurl
property PDFDocument : a reference to current application's PDFDocument

tell application "Keyboard Maestro Engine"
   set myPath to getvariable "Attaching File URL"
end tell

set myDoc to POSIX file myPath
set myPDFDocument to PDFDocument's alloc()'s initWithURL:myDoc

return myPDFDocument's |string|() as text

This sounds like a question for @ccstone.

1 Like

It went down from 6 seconds to 0.04 AMAZING! this makes a massive difference when we repeat this about 300 times a day :slight_smile:

Woo hoo! That’s awesome!

The script I presented months ago in post 19 of this thread runs in approximately 0.015 seconds – and properly looks for newest added file.

Using sort in the Finder like this script below will always disappoint, unless the number of files in the source folder is very low.

tell application "Finder"
   set fileList to (sort (get files of sourceFolder) by creation date)
end tell

It will always be slooow.

-Chris

1 Like