Script for saving path to recently saved file not working...

Hello, first post here!

I want to use create a script that can give me the path of the most recently saved file of a given folder. I found this script online but keep getting this error: Can’t get every file of “Users/brandonwentland/Pictures/Monosnap”. (-1728)

Here is the script:

tell application "Finder"
set latestFile to last item of (sort (get files of ("Users/brandonwentland/Pictures/Monosnap")) by creation date) as alias
set fileName to latestFile's name
end tell

Thanks!

Hi. Welcome to the forum.

As alias changes HFS paths”colon separated”but you have a posix path”slash separated. The coercion should also follow the path to modify it. Here are a couple options.


tell application "Finder" to set latestFile to last item of (sort files of ("Users:brandonwentland:Pictures:Monosnap" as alias) by creation date)
latestFile's name


--this may be more flexible (no hard-coded path)
set picsFldr to path to pictures folder as text
tell application "Finder" to (sort (picsFldr & "Monosnap" as alias)'s files by creation date)'s last item's name

Awesome, thanks so much! I got that working!

I want that file then to open in Monosnap. Here is what I have for code:

It opens in Preview instead of Monosnap however… I did a bit of research and apparently you can’t control an app with Applescript if it does not show up in the script dictionary (which it does not). Is there a work around or I’m I doing it wrong?

Hi,

the Finder can open files using a specific application, you need the bundle identifier of Monosnap.
Launch Monosnap and run this script


tell application "System Events"
	set monosnapIdentifier to bundle identifier of process "Monosnap"
end tell
display dialog monosnapIdentifier

then replace xxx.yyy.zzz with the proper identifier in this script


set picturesFolder to path to pictures folder
tell application "Finder"
	set latestFile to last item of (sort files of folder "Monosnap" of picturesFolder by creation date)
	open latestFile using application file id "xxx.yyy.zzz"
end tell

Hmm, getting this error:

/var/folders/tl/njskscg90zvdj49f4bfnhc0h0000gn/T/Keyboard-Maestro-Script-273D3324-388D-4EDB-8CE2-C7A2A5CD9803:72:73: script error: Expected "end" or "end tell" but found unknown token. (-2741)

Using this code:

set picturesFolder to path to pictures folder
tell application "Finder"
   set latestFile to last item of (sort files of folder "Monosnap" of picturesFolder by creation date)
   open latestFile using application file id "com.monosnap.monosnap"
end tell

Screenshot:

I tested the script in Keyboard Maestro and it worked. Check the double quote characters

Hello Stefan

One more time the message parser introduced extraneous invisible characters.
When I clicked on Open the scriplet in your Editor and tried to compile, I got : "

“Erreur de syntaxe”
« end » ou « end tell » prévu(s) mais jeton inconnu trouvé(s).

Which is the french wording for what the asker got.
It appear that such odd character was inserted where we were supposed to get a tab character.
I make a test inserting here the code after removing the offending gremlins.

set picturesFolder to path to pictures folder

tell application "Finder"
	set latestFile to last item of (sort files of folder "Monosnap" of picturesFolder by creation date)
	open latestFile using application file id "com.monosnap.monosnap"
end tell

After seeing my message in macScripter I clicked on Open the scriplet in your Editor and this time it compiles flawlessly.

Yvan KOENIG (VALLAURIS, France) jeudi 30 avril 2015 16:16:47

Thanks for pointing that out

Its working, you rock, thank you so so much! Would never had done this on my own (2 hours of tinkering).