I'm often creating PDF files from web pages in MacOS X (10.6) and very often I have the problem that the automatically proposed file name (based on the name of the web page) contains many special characters.

So I wonder: is there an easy way (apple script, shell script, OS X service) which takes a string (like this file name) and converts it by

  • removing all spaces and other special characters (also those which might be allowed in OS X, but forbidden in Windows or Linux!! as I'm often transferring files to our PC)
  • maybe starting each word (after a removed space) with an uppercase letter
  • ... what else could be important?

I'm using Quicksilver, so maybe it could be possible to create a command which makes it possible to replace the current text selection (in the file save as... dialog) by a converted string.

2 Answers 2


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):

enter image description here

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.

enter image description here

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'

enter image description here

  • 1
    How would one go about doing this in a Linux environment? (Or, better yet, in Cygwin.)
    – nitrl
    Jul 11, 2013 at 20:20

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .