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.

  • Note: The folder is on an NTFS volume on an external hard drive.
    – excitive
    Commented Sep 27, 2015 at 17:25

3 Answers 3


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

Using Finder to get lists of files can be problematic as well as long winded. I'd recommend to fall back to unix tools like this...

# Get list of files...
#   For example: all .txt files (not folders) under Downloads and below.
    set filelist to (do shell script "find ~/Downloads -type f -name '*.txt'")
on error eStr
    # Do something here...
    log eStr
end try

# Count the results
set n to count every paragraph in filelist

# Get a random no
set n to (get random number from 1 to n)

# Get the nth item from the results...
get paragraph n of filelist

Check out a short script (from another party) to see how you can use to rename files in an ascending order. This way you can get a sense of how indexes work. I made the script below that works for me. It sets the index of the files that are in the folder. In the folder I have each file named from file1.. file2.. file3..

The script works as follows: sets the index based on the number of files, then chooses a random number between the lowest number to the index of files and then I use the GUI with "System Events" to tell terminal to open a file with the random number.

Note: I take advantage of a wonderful tool from Terminal that with tab you can finish writing to the end of the intended file so no need to type the rest. Howto link

set text item delimiters to "."
tell application "System Events"
    tell application "Finder"
        set all_files to every item of alias "Macintosh:Users:Home:Documents:terminal:folder3notes" as list
        repeat with index from 1 to the count of all_files
        end repeat
        set randomnumber to (random number from 2 to index)
        say randomnumber
    end tell
    tell application "Terminal" to activate
    keystroke "open /Users/Home/documents/terminal/folder3notes/file"
    keystroke randomnumber
    key code 48
    delay 0.5
end tell
  • Why not just do it in a do shell script? Commented May 24, 2018 at 0:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .