August 1, 2012 at 9:56 PM by Dr. Drang
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.
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.
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
- switch to Safari,
- copy the URL,
- switch back to my editor, and
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.