Random Images and Automator

Hi - I’ve just started to use Automator to get a certain image from a Finder folder to add as an attachment to an outgoing email.

Is there a way to have Automator select a RANDOM image from a Finder folder or, indeed, from a designated album in the photos app, to attach to an email?

Many thanks for considering this.

All good wishes,


You could use a short set of shell commands to choose a random file from a directory, and then take the result of that to do the subsequent processing. Automator does support shell script.

This gets a list of the content of my Documents folder, removes any directories from the list, sorts it randomly, and then takes the first entry from the top of the list.

ls -p ~/Documents | grep -v / | sort --random-sort | head -n 1

It might also be worth reviewing the use of Automator for this vs say Shortcuts. If Shortcuts is an option for you to use, then you may find it even easier to do things like select a file at random.

Thanks again for coming back on one of my questions.

I have done the following:

  1. I have created a folder in my Documents called ‘Images’.
  2. I have adapted your shell script as shown in the attached image.
  3. The script seems to be working but gives me a file name rather than the image I would really want to add.

Can you advise on what I am doing wrong here.

Thank you very much for doing this for me. :slight_smile:

All good wishes,


Okay, so your original post suggested that you were selecting an image via your Automator workflow that would then add it as an e-mail attachment. As you included no details of your workflow, I was providing a way to get the filename for the file - i.e. the way to identify the random image. So as you implemented it, it is doing exactly what I intended it to, but this does not match into your overall process whereby it seems you want to copy an image to the clipboard and then paste it into an e-mail.

Here’s a revised script that when executed should choose the random file from your Images folder, and then put the file onto the clipboard using a bit of AppleScript. At least it does for me when I have just tested it on my laptop.


theFile=$(ls -p $theFolder | grep -v / | sort --random-sort | head -n 1)
osascript -e 'set the clipboard to POSIX file "'$theFilePath'"'

That should bring it down to just one Automator Run Shell Script action.

See if that gets you what you are after.

1 Like

Hi Stephen

First let me say I’m sorry if I misled you on my initial question. It wasn’t my intention. I’m not a coder and so my language may be imprecise.

Thank you very much for expanding the code for me.

I’m testing it later this week and will let you know how I get on.

Again, apologies for any misunderstanding.

All good wishes,


What would be the script if you are using it in Automator with the “Get Specified Finder Items” step before it? I tried it and it works if I put the items in the Documents folder, but I’d like to use any folder including any

external drives.

What are you trying to do? Select a random file from a folder or a random file from a set of selected files? If the former do you want to select a folder each time or just specify a different folder?

I’d like to select a random file from a folder which has hundreds of folders with thousands of image files in each of them. Is that possible? Or does having hundreds of subfolders stop the action?

Mac’s Screensaver seems to be able to pick random images and display them on multiple screens at any time from this same folder. So I thought it would be possible to instead of screen capturing the screensaver images (using a script I found for that) I can copy the original files and have them stored in a folder of my choosing to be able to use. This random picking is what I’m interested in. The screen capture wouldn’t have the image information plus there would be black areas around for vertical images.

Okay, so I’m not sure where the Get Specified Finder Files comes into this then. It sounds like you are always working from a specific folder. Therefore, you should just be able to change the path specified for theFolder to specify where the files are that you want to process. Do wrap in double quotes for any spaces, etc.

No, it just won’t look in those sub folders as is - just the folder you specify. To incorporate sub folders, you would need to set the listing to be recursive and remove blank entries.

Try setting the line that sets the value of theFile to the following.

theFile=$(ls -p -R $theFolder  | grep -v / | grep -v -e '^$' | sort --random-sort | head -n 1)

This includes changes to do both of those things mentioned above.

Hope that helps.

1 Like

Might be easier for the OP to just use “some” AppleScript :wink:

tell application "Finder"
	set theFolder to choose folder
	set theFile to some item of (get files of theFolder)
end tell
1 Like