I need an AppleScript to open the newest file in a folder.
When I try this:
set theFolder to choose folder
tell application "Finder"
set theFile to select last item of (sort files of theFolder by name)
activate
open theFile
end tell
This works fine. But when I put the path to the same folder into the script itself, like this:
set theFolder to "Mac HD:Users:bartus:Documents"
tell application "Finder"
set theFile to select last item of (sort files of theFolder by name)
activate
open theFile
end tell
I keep getting this error message:
every file of “Mac HD:Users:bart:Documents” cannot be requested.
I have tried it on a Mac Mini running Mojave and on a MacBook Pro running Monterey. Both say the same thing. Neither does it matter how I sort the folder (creation date, name).
I have given ScriptEditor permission to control the computer in the Accessability pane of the Security Settings.
I have tried exporting the script to an app and run that, to force it to get permission from the automation pane, but it never seems to even get there, it just displays the error message.
How can I get this to work?