The Safari 6 URL crisis

There’s a small crisis spreading across that part of the Mac world that’s upgraded to Mountain Lion and/or Safari 6. The crisis has been triggered by this behavior: when you enter search terms in the omnibar and go to the Google results page, the search terms don’t get replaced by the URL of the Google page.

Safari 6 search

How is this a problem? Suppose you want the URL of that Google results page; you can’t just swipe through the field and copy it because it isn’t in the field to be swiped.

People soon learned that the URL is available via AppleScript, so there’s been a flurry of posts and tweets with scripts that look something like this:

tell application "Safari"
  set theURL to the URL of the current tab of the front window
  set the clipboard to theURL
end tell

How the script gets run depends on the tools you have. There’s the venerable Scripts menu that Apple provides and a host of third-party solutions, including FastScripts, LaunchBar, Quicksilver, and TextExpander.

The interesting thing about this solution is its unnecessary reliance on the clipboard. The programmers are duplicating their old “by hand” behavior instead of taking advantage of what scripting can do for you. And it’s costing them a step or two every time they use their script.

(I’m assuming here that the people using this technique have TextExpander, which is probably a safe bet. It’s the best tool for this job.)

Here, for example, is what an AppleScript TextExpander snippet that gets the URL of the frontmost tab should look like:

tell application "Safari" to get the URL of the front document

Boom. Give that a nice abbreviation, like ;furl for “front URL,” and it’ll insert the URL directly where you’re typing. There’s no need to run the script and then paste the URL—that’s an inefficiency that comes from thinking the script has to act like you would.

There’s a famous aphorism by Edsger Dijkstra that covers this situation:

The question of whether a computer can think is no more interesting than the question of whether a submarine can swim.

I’ve been using ;furl for years to insert URLs into what I’m writing without having to

  1. switch to Safari,
  2. copy the URL,
  3. switch back to my editor, and
  4. paste.

That it’s a solution to this new Safari 6 problem is just an added benefit.

Once I got comfortable using the ;furl snippet, I made several others that get and insert the URLs from tabs that aren’t necessarily frontmost. ;1url gets the URL from the leftmost tab, ;2url gets the second one from the left, and so on up to ;6url. All of these snippets are in this GitHub repository.

By the way, if you look at the snippets in the repository, you’ll notice that they’re more complicated than the one-liner given above. That’s because I wrote them back when I was considering a switch to Chrome and I wanted the snippets to work with both browsers. They do.

10 Responses to “The Safari 6 URL crisis”

  1. Lauri Ranta says:

    I still use a script like this:

    tell application "Safari" to URL of document 1
    set the clipboard to result

    I often paste URLs from one Safari tab or window to another, which would be more difficult with snippets like ;2url. The URLs will also be added to Alfred’s clipboard history, so I can search for them later or quickly copy two or more URLs in a row.

  2. Charlie says:

    I just use nvALT’s Safari extension to copy the URL to a new note.

    Crisis averted.

  3. Dr. Drang says:

    Actually, Lauri, one of my prime uses of the ;nurl snippets is to insert the URL of one tab into a field of another. It’s not difficult at all. It is true that the URL isn’t kept in a clipboard history, which would be a small annoyance if I often needed to insert the URL more than once. For me, that doesn’t come up often enough to worry about.

  4. has says:

    What, no love for Services menu? I’m only surprised OSX doesn’t include an ‘Insert URL from Safari’ service as standard.

    1. In Automator, create a new workflow using the ‘Service’ template.

    2. Set ‘Service receives selected […]’ option to ‘no input’. Check ‘Replaces selected text’.

    3. Add a ‘Get Current Webpage from Safari’ action to the workflow.

    4. Save service as ‘Insert URL from Safari’.

    5. [Optional] Select ‘Services Preferences…’ in the Services menu, scroll down the list of keyboard shortcuts to the ‘Insert URL from Safari’ entry in the ‘Text’ category, double-click it and press your preferred key combo.

    Congrats, you can now insert Safari URLs directly in any application [1] that supports Services (some Carbon-era throwbacks don’t, but most do). You can run it via the application’s Services sub-menu, contextual menu or [if you set it up] keyboard shortcut.

    [1] One place it definitely can’t work is - not surprisingly - within Safari itself. It harmlessly times out after a few seconds if you try, but if the unhelpful error dialog bothers you it’d be easy enough to use some scripting ju-ju in the workflow to confirm Safari isn’t frontmost before proceeding to get its URL. Ditto if you want nicer behaviour (e.g. a simple beep) if there’s no Safari windows open, etc.

  5. Zack Katz says:

    I created the Get URL Safari Extension that allows you to easily copy the current URL of the tab. It is easier than creating an Automator service and prettier than a bookmarklet!

  6. Doug Adams says:

    That you thought to approach this task the way you did with TextExpander displays a refreshing use-case for good ol’ AppleScript in The Modern World. Thanks.

  7. Josh says:

    Easiest answer: turn on the Developer menu, it’s under the Advanced prefs. Then just use Web Inspector to see/copy/grab the current page’s URL!

  8. Charles Pergiel says:

    I am continually amazed by what people will spend their time on. My solution is to not use Safari. I am probably missing something wonderful, but since I don’t know what it is, I don’t miss it. Yay!

  9. Dr. Drang says:

    You’re right, Charles, it is amazing what people spend their time on. Why, for example, would anyone bother to leave comments on posts he thinks are uninteresting?

  10. pavelal says:

    No Develop menu/extensions/scripts/etc required. Do you see the icon with the magnifying glass in the address/search bar? Just drag it to your editor, it will drag the text containing the real URL. Still, I love the old way more:-)