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?
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.
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.
#!/bin/zsh
theFolder=$HOME/Documents/Images
theFile=$(ls -p $theFolder | grep -v / | sort --random-sort | head -n 1)
theFilePath=$theFolder/$theFile
osascript -e 'set the clipboard to POSIX file "'$theFilePath'"'
That should bring it down to just one Automator Run Shell Script action.
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
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.