Quarter Life Crisis

The world according to Sven-S. Porst

« BrowsingMainMailboxer v4 »

Escaping URLs

449 words on

John Gruber writes about escaping JavaScript for your bookmark URLs and comes up with a scheme to automate that. It uses BBEdit and perl. Not only are those among my least favourite tools – I never got the hang of BBEdit and rather despise perl because of its unreadability, but this approach seems rather Mac-unlike to me with its use of Unixoid scripting languages and its limitation to a single application (although John does point out the option of using the script in a Service).

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.

Image of UnicodeChecker's services menu

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
process(the result)
set the clipboard to the result

on process(theURL)
    if theURL begins with "javascript:" then
        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
    else
        tell application "UnicodeChecker" to escape theURL to URL
        get "javascript:" & the result
    end if
    return the result
end process

Open this script in a new Script Editor window.

And then, we could wrap it up to give yet another Service using ThisService, say.

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.

March 21, 2007, 0:32

Tagged as software.

Add your comment

« BrowsingMainMailboxer v4 »

Comments on

Photos

Categories

Me

This page

Out & About

pinboard Links

♪♬♪

People

Ego-Linking