Here's the second half to my post regarding automatically adding file extensions to files in OS X.
--set the list of characters you want to replace
--disallowedChars will be replaced with the replacementChar
--in this case, an underscore
property disallowedChars : ":;,/|!@#$%^&*()+"
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 : "_"
property replacementCharacter : "_"
on adding folder items to this_folder after receiving added_items
tell application "Finder"
try
repeat with x in added_items
try
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
else
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.
10 comments:
I would also like to remove quotation marks from file names. How do you do that?
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.
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.
dlydiamg
I get an error:
Expected end of line, etc. but found unknown token.
Help please!
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.
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!
supper Thanks!!!
But I was alsow looking for a script modify all folders and files below the selected folder.
Is that simple to fix.
Hi,
Works nicely!
Hi
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
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.
Thanks
rslygh
Post a Comment