Open any URL from any app with Butler and AppleScript

Jun 07, '07 07:30:03AM

Contributed by: Anonymous

One of my favorite productivity apps is Butler. While the "Which launcher app is better?" debate will rage interminably, I especially like Butler's ability to perform web searches simply via a keyboard shortcut activated pop-up bezel interface. All I have do to is hit Command-Option-Control-G, and a bezel box opens, allowing me to perform a Google search no matter what app I'm in. Ditto for -M and Google maps, -V and Version Tracker, etc.

I found myself wanting the ability to assign a simple keyboard shortcut, get a dialog box, and open any URL I wanted -- no reaching for my browser. While Butler contains a way to do this, it generally requires a step or two more than I'd like, and I wanted a more direct solution. Using one of Butler's most powerful features -- the ability to execute any AppleScript code with only a keyboard shortcut -- I implemented the following code:

display dialog "URL?" default answer "" buttons {".net", ".org", ".com"} default button 3
set {text returned:theURL, button returned:theDomain} to result

if theURL is "" then error number -128 -- cancel

if (offset of "." in theURL) is 0 then
  -- This works for other domains, as long as no subdomains are entered
  set theURL to theURL & theDomain
end if

if theURL starts with "www." then
  set theURL to "http://" & theURL
else if theURL does not start with "http://www." then
  set theURL to "http://www." & theURL
end if

tell application "Safari"
  activate
  open location theURL
end tell

(With a little revision from our friends at macscripter.net)

I've assigned this to the key combination Command-Option-Control-U, and now I can open a URL no matter what application I'm in or what I'm doing -- and with almost no attention necessary to the prefix or suffix.

[robg adds: This is a nifty script, and it worked as described, although I changed the tell application "Safari" bit to open Camino instead.]

Comments (12)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20070602121144514