AppleScript for Making Clipboard Contents Filename Ready
Today I wanted to go through my downloads folder and clean things up a bit. I have a lot of random pdfs in that folder with random names like "fulltext(1).pdf" and "fultext(9).pdf" (thanks IEEE eXplorer, or was that ACM) and others with equally informative names like "S1350482701003061a.pdf". Clearly these simply won't do. What should the filename be? Well the paper/documents title of course! If you open up the pdf and copy the title of the paper you can't paste it as the filename as it might have have undesirable characters in it, especially new lines. I'm rather picky about my file names. I don't want any characters other than a-z, 0-9, . (period) , _ (underscore), and - (dash) in them. One could simply retype the title removing all the offending characters, but that is really tedious, especially with massive titles. So what is one to do? Use AppleScript to sanitize the contents of the clipboard so that you can paste it as the file name.
I tried several different techniques. Initially I wanted to use sed or tr or some other straight forward unix tool, but pbpaste wasn't cooperating. It simply wouldn't show anything. After reading up on pbpaste its rather limited with regards to the types of content it can paste form the clipboard and apparently the contents I had that I was testing with wasn't one of them.
Hence, I opted for a pure applescript approach . . . why doesn't applescript have string manipulation functions? I mean really?! So after googling around for a bit I ended up with satimage's applescript library for string manipulation. A few regex's later and I had an acceptable script. Since I use QuickSilver its simple for me to copy text then run the applescript and past things into the filename.
The script file can be downloaded here, or the source code is below: