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

Script or automation to change part of a file name

My apologies for making my first post a question, rather than a reply. I am trying to write or find a script that will allow me change the case of only part of a file.


Scenario:

I receive pdfs from a webform. The files are renamed automatically (via Hazel) to include 7 characters at the beginning, followed by a series of characters some of which end up being in uppercase.

eg: PP4-567_EDWARD&Amelia.pdf or PP4-567_EDWARD&AMELIA.pdf or PP4-567_Edward&AMELIA.pdf


Outcome:

In this instance I'd want to change EDWARD to Edward, without changing the 'PP' at the beginning of the filename. And of course, sometimes the second name "Amelia" is in uppercase instead or as well.


Question:

Is there a way to do it?


Where I am at:

Could it be done with Automator (watching a folder), or some other way? I could use Keyboard Maestro, but that seems like overkill for something that happens only sometimes. I could just do the changes by hand, but often a problem file comes in when I'm not at my mac, so automating this is important to me.

I've tried on my own (scripting skills are minimal), searched the web for help (can't choose between AppleScript, python, java or even know the best option). Am willing to learn and search more, and would appreciate pointers, since I'm already confused and discouraged enough!


Thanks for even looking at this request!

Cheers

Joss





iMac Pro

Posted on Oct 14, 2019 9:30 AM

Reply
Question marked as Best reply

Posted on Oct 16, 2019 3:59 PM

This works perfectly as a folder action! Thank you so much! As an experiment (for my learning) I tried adding the script into a Hazel action, which worked somewhat, only it gave me some funky results with the name. (Not a problem, as I said, I was experimenting.)


Meanwhile! This is a perfect solution, thank you!

Similar questions

11 replies
Question marked as Best reply

Oct 16, 2019 3:59 PM in response to VikingOSX

This works perfectly as a folder action! Thank you so much! As an experiment (for my learning) I tried adding the script into a Hazel action, which worked somewhat, only it gave me some funky results with the name. (Not a problem, as I said, I was experimenting.)


Meanwhile! This is a perfect solution, thank you!

Oct 16, 2019 9:28 AM in response to jolomero

If you assign your Box folder location on the desktop as the designated folder for the folder action, then when you drag and drop the original files onto the Box folder, this script is currently written to place the capitalized filenames in the Box folder.


Launch Automator, and choose a new Folder Action. Set the name of the target folder of the Folder Action to your Desktop Box folder name, not the Ztest that I used. Drag and drop the Run Shell Script action from the Automator library onto the larger workflow window. Remove any boilerplate that appears when you set the Shell: to Zsh and Pass input: as arguments.



Now, copy/paste the following Zsh shell script code into the Run Shell Script action, and Save. I gave my folder action the name: Capitalize Filename. The folder action is stored in ~/Library/Workflows/Applications/Folder Actions. You can edit from the Automator Open Recent menu. Also, this script is not setup to process folders that are dropped on it.


# Capitalize parts of filename string
# PP4-567_EDWARD&AMELIA.pdf becomes PP4-567_Edward&Amelia.pdf

# process all files dropped on the target folder
for f in "$@"
do
	# split filename using these delimiters into the respective variables
	IFS='_.' read pre names ext <<<"${f}"

	# The C means capitalize any word in names that needs it
	# A U would be uppercase, and an L would be lowercase
	newfile=${pre}_${${(C)names}:t}.${ext}

	# rename original to capitalized name in the designated drop folder location
	mv $f ${f:h}/${newfile:t}
done



Oct 14, 2019 2:51 PM in response to jolomero

Is the name format always an underscore preceding the first name (e.g. EDWARD)? Are the two names always separated by the ampersand character? Is the second name always followed by the extension? If all are true, then that forms a pattern where one can capture the two names and apply Titlecase to each name before the renaming operation.

Oct 15, 2019 11:30 AM in response to VikingOSX

Hi VikingOSX - I have no clue about Zsh, but very willing to give it a whirl now that I know it's Linux/Unix.


And thank you for noticing the pattern. To answer your question - yes there is "always" an underscore before the first name and an ampersand between them. (It helps with post-processing using Zapier).


I would love to give your script a try!


Oct 15, 2019 4:41 PM in response to jolomero

I do not have Box, so cannot test that. However, if Box appears on your Desktop as a folder, then all the script needs is that destination path to Box, and the renamed file can be moved directly into Box. As presently written, the script moves the dropped file(s) from the original filesysem location into the folder action folder, where it is renamed with the capitalization applled.

Script or automation to change part of a file name

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