Tuesday, April 14, 2009

Applescript to automatically replace characters in filename

Here's the second half to my post regarding automatically adding file extensions to files in OS X.

If you're in a mixed environment where Mac and PC users are, you know that it can sometimes be a challenge to get everyone to name files so that they are compatible with both types of computers. Normally the Mac users are the culprits because Macs allow a wider range of characters in the filenames. Configuring folder actions, and using a simple Applescript, you can automatically remove any characters you want from the file names though, or replace them with a different character of your choosing. Here's the Applescript:

--set the list of characters you want to replace
--disallowedChars will be replaced with the replacementChar
--in this case, an underscore
property disallowedChars : ":;,/|!@#$%^&*()+" 

--anything in disallowedChars2 will be removed altogether
property disallowedChars2 : "'"

--set the character you'd like to use to replace the invalid 
--characters specified in disallowedChars
property replacementCharacter : "_"

on adding folder items to this_folder after receiving added_items

tell application "Finder"
repeat with x in added_items

set fileNamed to name of x
set newName to my CleanName(fileNamed)
set (name of x) to newName

end repeat
on error
display dialog "An error occurred."
end try
end tell

end adding folder items to

--function for cleaning the characters from the file name
on CleanName(theName)

set newName to ""
repeat with i from 1 to length of theName

--check if the character is in disallowedChars
--replace it with the replacementCharacter if it is
if ((character i of theName) is in disallowedChars) then
set newName to newName & replacementCharacter

--check if the character is in disallowedChars2
--remove it completely if it is
else if ((character i of theName) is in disallowedChars2) then
set newName to newName & ""

--if the character is not in either disallowedChars or
--disallowedChars2, keep it in the file name
set newName to newName & character i of theName

end if
end repeat

return newName
end CleanName

Using that script in combination with OS X folder actions will allow you to successfully replace any character you want with any character you specify. The only things you'd need to modify are the disallowedChars, disallowedChars2, and replacementCharacter variables. 


buzzwig said...

I would also like to remove quotation marks from file names. How do you do that?

rslygh said...

Quotation marks pose a little problem because they're normally used to show that something is meant to be viewed as text. I have not tested this suggestion, but you could try adding \" to the disallowedChars variable. I believe the \ is used as the escape character in Applescripting, as it is in many other programming languages. I'll try to test this and update, or if you beat me to it, please let me know if it solved your issue.

rslygh said...

To answer your question, \ does act as the escape character, so adding \" to either disallowedChars or disallowedChars2 will replace or remove quotation marks from your file name. I just tested it and it worked perfectly.

Anonymous said...

I get an error:

Expected end of line, etc. but found unknown token.

Help please!

rslygh said...

If you copied and pasted the code into your script, make sure everything lined up correctly. The line breaks may not have all worked during your copy/paste, so some of the code might have been shifted around. If that's not it then I'd need some more information regarding your error. Also, I know this worked on a 10.4 OS X server, but if you're using 10.5 or 10.6 I can't confirm nor deny any type of compatibility.

Anonymous said...

this is great and working well. many thanks!

i was wondering if you have any tips or suggestions to have this script modify all folders and files below the selected folder.

thanks again!

Remko said...

supper Thanks!!!

But I was alsow looking for a script modify all folders and files below the selected folder.

Is that simple to fix.

Jaap Schokkenbroek said...


Works nicely!

Joel Stonge said...


How does this work ?

I copy it in applescript and Run it, but it does no action…

How do I tell it: go get every item in THIS FOLDER ?

Thanks in advance

rslygh said...

Hi Joel,

It's been over 5 years since I posted this and actually used it, but I'll give it a shot at explaining. You create the script, which will then act like an executable if you were in a Windows environment. You then have to activate Folder Actions in Automator on the folder you want to use the script on, which allows you to assign the script to the folder you want. I did not know how to add any type of prompt to allow the folder to be selected at run time. So your THIS FOLDER is whichever folder you setup folder actions on.

Hopefully that helps.