function to automatically remove special characters from file names during saving in MacOS X
You can create an Automator service to do this in all applications that support services (which is almost all of them).
Open Automator and select to create a Service that receives selected text in any application and check Output replaces selected text.
Add a single Run Shell Script action from the library, and paste the following script code:
sed 's|[^a-zA-Z0-9]||g'
This specific script, a simple sed
substitution will remove all non alpha-numeric characters from the file name. It uses a-zA-Z0-9
as a whitelist of allowed characters, add to it as desired.
You can do other actions as well, and chain them together. For example, tr [A-Z] [a-z]
will lowercase everything, and sed 's|[^a-zA-Z0-9]||g' | tr [A-Z] [a-z]
will combine the two.
Save as e.g. Sanitize Filename, and optionally assign a keyboard shortcut in System Preferences » Keyboard » Keyboard Shortcuts » Services. You should be able to invoke services using Quicksilver as well, just type their name.
Whenever you're in a Save as… or similar dialog, select the suggested file name (it's selected by default):
Invoke the service Program Menu » Services » Sanitize Filename (or use the keyboard shortcut you invoked). It will pipe the file name through the script, and use its output to replace the selection.
For title-casing and removing bad characters (I also keep underscore, dot and hyphen in), the following script code seems to mostly work for me:
perl -ane 'foreach $wrd(@F){print ucfirst($wrd)." ";}' | sed 's|[^a-zA-Z0-9_.-]||g'
Same idea as Daniel had. Create a Service in Automator. Replace the Run Shell Script shell with /usr/bin/ruby
, and add the following:
input, output = STDIN.read, ""
input.each("\s") {|word| output << word.capitalize}
puts output.gsub!(/[^0-9A-Za-z\-]/, '')
This will get you the capitalization and you can do a bit more, of course, if you know Ruby. You could trigger the Service using QuickSilver but you would need to activate your browser using AppleScript before, I guess.