MacOS – Selecting Random File in AppleScript

applescriptfindermacos

I’m trying to make a script that can select/display/open a file randomly from a folder and its subfolders. The following script:

tell application "Finder"
    get some file of (folder "/Volumes/Expansion Drive/Photos")
end tell

Gives following error:

error "Finder got an error: Can’t get folder \"/Volumes/Expansion Drive/Photos\"." number -1728 from folder "/Volumes/Expansion Drive/Photos”

What is the correct syntax? It’s a late 2013 iMac with OS X 10.10.5.

Best Answer

Because you're using the POSIX path syntax, you have to use POSIX file to get the object at that path:

tell application "Finder"
    get some file of (folder (POSIX file "/Volumes/Expansion Drive/Photos"))
end tell