449 words on Software
So I’ll just point out a simpler and more versatile tool for the same job: Our very own UnicodeChecker. If you have it on your system already you will find its ‘Unicode’ submenu in every application’s ‘Services’ menu And that menu contains the Service ‘Add Percent Escapes’ which handles the technically important aspect of what John’s script does – escaping the spaces and other ‘special’ characters for use in an URL.
This feature already leaves a range of characters [ASCII alphanumerical and !$&’()*+,-./:;=?@~ if you must know] unescaped, so default legibility should be all right. UnicodeChecker services also include a ‘Replace Percent Escapes’ item that undoes the conversion, so you can easily edit the URL later on.
Of course we can beef this up a little and wrap it into an AppleScript that handles both directions of the conversion. Perhaps something like this:
get the clipboard
set the clipboard to the result
set AppleScript’s text item delimiters to (":")
tell application "UnicodeChecker" to unescape theURL from URL
get (rest of text items of the result) as string
tell application "UnicodeChecker" to escape theURL to URL
return the result
And now people can start pointing out that I omitted all that additional nice code which John used – like the removal of excessive whitespace and so on. To be honest I was hesitant to do that because it would make the script far less invertible. Undoing the escaping would then not give back what we started with. Besides, just as regular expressions are easy to use in perl and perfectly match its obfuscated style, doing similar replacements in AppleScript is so horrible that you intuitively start avoiding them when entering AppleScript mode.
If you liked this, you may also be interested in AppleScript CSS validation which works along the same lines.