You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Really Basic Automator Rename Help Please

Hi,


I made an Automator workflow some years ago that used to work, but now it doesn't seem to and I cant figure out why. Any help would be appreciated, I realise this is probably incredibly basic but this is the only time ive used Automator.


I have frequently have different versions of images, denoted by a _V1, _V2 _V3 etc Suffix. I just need a quick way to remove these varying suffixes and replace them with _RGB. I currently have a series of RENAME FINDER ITEMS to find the varying V numbers and remove them, culminating in a RENAME FINDER ITEM to add _RGB as shown below.


When multiple files are dragged and dropped this only seems to work on anything with _V1 and it is renamed to _RGB and all the others stay the same. If I only drag _V2, it deletes the suffix but doesnt add _RGB


THanks for any help

Mac Studio

Posted on Jun 6, 2022 6:13 AM

Reply
Question marked as Top-ranking reply

Posted on Jun 6, 2022 2:26 PM

Here is another approach to using Automator in processing custom filenames as you have shared. It can handle multiple selections of files and folders, and process your image pattern rename in each file encountered. It is extended for other image formats than jpg, and if you have RAW files, you can add those as lower case extensions too.



Run shell script code:


: <<'COMMENT'
Given files and folders as input, convert filenames from imagenn_vnn.ext to imagenn_RGB.ext.
If encountering a folder, then process all image files in its first level w/o recursion.
COMMENT

autoload -U zmv

function rename_file () {

	# rename imagenn_Vnn.jpg to imagenn_RGB.jpg using zmv pattern.
	zmv ${1} ${f//_[Vv][0-9]*/_RGB.${2}}
	return
}

function rename_folder_contents () {
	# case insensitive search of images in the folder.
	setopt NO_CASE_GLOB
	for f in ${1:a}/*.(jpg|jpeg|png|tif|tiff)(.N);
	do
		$(rename_file "${f}" "${f:e}")
	done
	return
}

for f in "$@";
do
	# is this a folder or a file?
	# pass folder to its function, filename and extension to its function
	if [[ -d "${f:a}" ]]; then
		rename_folder_contents "${f:a}"
	else
		rename_file "${f:a}" "${f:e}"
	fi
done
exit 0


Tested with Automator on macOS 11.6.6 and Zsh shell v5.8.

Similar questions

7 replies
Question marked as Top-ranking reply

Jun 6, 2022 2:26 PM in response to DRRamsay

Here is another approach to using Automator in processing custom filenames as you have shared. It can handle multiple selections of files and folders, and process your image pattern rename in each file encountered. It is extended for other image formats than jpg, and if you have RAW files, you can add those as lower case extensions too.



Run shell script code:


: <<'COMMENT'
Given files and folders as input, convert filenames from imagenn_vnn.ext to imagenn_RGB.ext.
If encountering a folder, then process all image files in its first level w/o recursion.
COMMENT

autoload -U zmv

function rename_file () {

	# rename imagenn_Vnn.jpg to imagenn_RGB.jpg using zmv pattern.
	zmv ${1} ${f//_[Vv][0-9]*/_RGB.${2}}
	return
}

function rename_folder_contents () {
	# case insensitive search of images in the folder.
	setopt NO_CASE_GLOB
	for f in ${1:a}/*.(jpg|jpeg|png|tif|tiff)(.N);
	do
		$(rename_file "${f}" "${f:e}")
	done
	return
}

for f in "$@";
do
	# is this a folder or a file?
	# pass folder to its function, filename and extension to its function
	if [[ -d "${f:a}" ]]; then
		rename_folder_contents "${f:a}"
	else
		rename_file "${f:a}" "${f:e}"
	fi
done
exit 0


Tested with Automator on macOS 11.6.6 and Zsh shell v5.8.

Jun 6, 2022 11:53 AM in response to Camelot

Thanks for your help. All the filenames will be different, they just share the common suffixes. I think what I require is linear, I am just explaining it poorly.


I did paste a screengrab in but I see now its not there. My workflow looks like this screengrab below. It starts finding _V1 and proceeds identically all the way until _V20 as you see here. It then should add _RGB to all the files.


I have also tried making each section add the _RGB as it goes, but thats doesn't work either. As mentioned before, it seems to work perfectly for _V1 but none of the rest.

. Its starts with a rename from V1



Jun 6, 2022 11:36 AM in response to DRRamsay

Without seeing your workflow it's impossible to predict what's not working, or what changes might need to be made.


Additionally, as described, the script is doomed to fail if there are multiple Vx files passed in.


For example, if you have image_V1.jpg, image_V2.jpg and image_V3.jpg, your workflow should change image_V1.jpg to image_RGB.jpg - fair enough. But the file image_V2.jpg should then also be renamed to image_RGB.jpg. Should that replace the existing file? fail gracefully? ask the user? etc.?


Ultimately, though, Automator is great at linear tasks. It's less suited to workflows that require looping or logic filters. Not to say it can't be done, but it takes more effort.

Jun 6, 2022 4:08 PM in response to DRRamsay

The reason why the workflow only processes the _V1 files is a perfect example of how/why Automator fails when there's any kind of logic processing or iteration.


To explain, your first step renames items that have _V1 in their name. That's fine, and what you what.


HOWEVER, the OUTPUT of this command (in short, a list of the files that were renamed) are then passed in to step 2.

To be clear, NONE of these file names will contain _V2 because this is a list of the files that were renamed from _V1 to _RGB


You see, the issue is that that output of one step is passed as the input to the next step.


What you need is for *each* of the steps to process the full list of files. That adds a degree of complexity.


In this case it probably hinges around taking the input list of files and storing that in a variable (via a 'Set value of variable' action). Then your workflow becomes a series of 'Get value of Variable' passed into a 'Rename Finder Items' action.

Now each rename will act on the full list of files, rather than the list of files that were just renamed.


Alternatively, as Viking shows, extracting the logic part to a shell script (or an AppleScript) avoids having to play such games.

Jun 13, 2022 2:00 PM in response to Camelot

Hi Camelot,


Thanks very much for your help, I really do appreciate it.


I understand what you are saying about how only the outputs from Step one are passed to Step two, the thing I find strange is that I used to use this app all the time and it worked. I have also just got out my old 2017ish MBP and found the original app, it still works on that computer. Was there some change to automator recently? If I transfer the same app onto my new M1 Max Mac studio it does not work.



Really Basic Automator Rename Help Please

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.