Automator Workflow Including a Move File to a Variable Location

I have an Automator workflow that is saved as an application. It runs at the end of an AppleScript with a Tell - Activate command.

The Automator action exports an image from Photos, renames and resizes the image, then moves it to the desired folder. This is necessary because Automator always and only exports to a new folder in the User’s Pictures folder.

Everything works fine if I use the Move Finder Items and designate the desired folder within that action through the dropdown.

What I would like to do is be able to move the image to a folder where the pathname varies with the current year. That is, the application works fine now. In 2025 I’ll have to edit the action to point to another Images folder within a 2025 folder. I would like to “automate” the path selection.

I thought I’d try a Shell Script Variable and set the path like this,

current_year=$(date +‘%Y’)
path=“/Users/daverogers/Documents/Tinderbox Files/Nice Marmot Exports/Archives/${current_year}/Images”
echo $path

(I’m not sure if this is correct, I’m using ChatGPT here. It’s 2024, it’s what all the cool kids are doing.)

The first problem is that it seems you’re supposed to be able to place a shell script within a shell script variable. When I open the shell script variable, I get this:

There is no way to enter a shell script into that field.

I don’t know if Automator is broken or what (I’m on Sonoma 14.3.1).

Failing this, would it be better to run some other script within a “Run Shell Script” action, or perhaps a “Run AppleScript” command, to actually move the file to the relevant folder based on the current year?

I’m uncertain if Automator would execute the script on the image file within the workflow automatically. I could experiment, but I’ve been futzing with this for a while now and I’m getting a little impatient.

As it is, everything works fine, but if I could just make it pick the current year folder automatically, I think it would be much more satisfactory.

Here’s what the current workflow looks like, though the “working” version has the name Image for the Move Finder Items action.

Appreciate any insight or advice.

Thanks.

This is a pure AppleScript solution.

  • It creates the destination folder including the current year if it doesn’t exist

  • It exports the selected media items to the destination folder

  • It resizes the images to 1600

  • It renames jpeg as JPG

tell application "Photos" to set selectedItems to selection
if selectedItems is {} then return

set currentYear to year of (current date)
set destinationPath to (path to documents folder as text) & "Tinderbox Files:Nice Marmot Exports:Archives:" & currentYear & ":Images:"
do shell script "mkdir -p " & quoted form of POSIX path of destinationPath

set fileReferences to {}
tell application "Photos"
	repeat with anItem in selectedItems
		if class of anItem is media item then
			set theName to filename of anItem
			set end of fileReferences to destinationPath & theName
			export {anItem} to file destinationPath
		end if
	end repeat
end tell
repeat with aReference in fileReferences
	do shell script "/usr/bin/sips -Z 1600 " & quoted form of POSIX path of aReference
	tell application "Finder" to set fileName to name of file aReference
	if fileName ends with "jpeg" then
		set newName to text 1 thru -5 of fileName & "JPG"
		do shell script "mv " & quoted form of POSIX path of aReference & space & quoted form of POSIX path of (destinationPath & newName)
	end if
end repeat
1 Like

Just a couple of points…

Try rearranging or resizing your window. On my system, the ‘variable’ window has a fixed size and location relative to the automator window. This is what it could look like. By the way, with the cursor in the ‘Name’ field, hit ‘tab’ once. This should move the cursor into the ‘Script’ text area. Perhaps you could then paste your script into the space.

Regarding variables, there is a ‘year’ variable in automator so you shouldn’t need to go to these lengths.

Regarding the shell script, assuming that you don’t get any toxic results from your AI, try running the commands in the terminal. You can then see for yourself whether they work or not. I suspect that there would likely be some online shell widgets (or whatever they call them) that you could test with. Also, 2023 was the year that chatgpt was cool.

Finally, automator works by stringing actions together, with each action typically working on the previous action’s results (and only the preceding action’s). You can see this relation when the two actions are joined together visually, as the ‘move finder items’ is joined to both the preceding and subsequent action in your screen shot. You can right-click on an action and tell it to ignore its input, if desired. When you run the workflow, you can click on the ‘results’ button for any action and see what it has passed on to the next action.

1 Like

Brilliant! I couldn’t get the variable window to expand no matter what I did with the Automator workflow window. (Seemed like a good idea, I like to keep the window as tall as possible.) But the tab and paste idea worked! As did the shell script. Looks like everything should work. I’m working in a test workflow, I’ll move it over to the full stack tomorrow.

Thanks for the idea about tabbing into the window.

Appreciate the response and all the effort. There’s a bit more to the AppleScript, but I can use this and try to integrate the stack into an AS-only solution if for no other reason than my own education.

The subsequent response got me into the Automator Shell Script variable via the tab-button (Duh! Why didn’t I think of that?) And a blind paste of the shell script worked, moving the image to the 2024 folder.