Use AppleScripts to send URLs from browser to browser

Mar 29, '06 06:24:00AM

Contributed by: ra5ul

I finally got around to generalizing the ability to "open URL in $browser" using a single keystroke -- that is, while running browser X, open the currently-viewed URL in browser Y. The services menu, unfortunately:

Other built-in methods were either inconsistent or incomplete, so I went with AppleScript, based on the five browsers I use (Camino, Firefox, Opera, Safari, and the lovely ELinks).

Part 1: The Scripts

Here's the AppleScript that handles grabbing the current URL and then passing it to the browser of choice:

tell application "System Events"
  set myApp to name of first application process whose frontmost is true
  if myApp is "Camino" then
    tell application "Camino"
      set myURL to URL of window 1
    end tell
  else if myApp is "firefox-bin" then
    tell application "Firefox"
      set myURL to «class curl» of window 1
    end tell
  else if myApp is "Opera" then
    tell application "Opera"
      set myInfo to GetWindowInfo of window 1
      set myURL to item 1 of myInfo
    end tell
  else if myApp is "Safari" then
    tell application "Safari"
      set myURL to URL of document 1
    end tell
  else
    return
  end if
end tell

do shell script "open -a camino " & quoted form of myURL
(Note: Thanks to this forum thread for the Firefox solution.)

I made four copies of the script, substituting camino in the final line with the appropriate browser.

For ELinks, the entire line should be replaced with this:
tell application "Terminal"
  activate
  do script "elinks " & quoted form of myURL
end tell
Also, Opera 9.x isn't AppleScript enabled by default (for now), so you have to add the following to its info.plist file:
<key>NSAppleScriptEnabled</key>
<string>Yes</string>
Finally, going from ELinks to a GUI browser is best handled within elinks itself. Here's the lua code to add to hooks.lua, e.g.:
function guiBrowser ()
  os.execute("open " ..current_url ())
end

bind_key ("main", "X", guiBrowser)
Which will open in your default browser.

Part 2: Key Bindings

I used QuickSilver to bind these scripts to unique keystrokes (Butler, etc. would probably work equally well). FastScripts (Lite) allows application-specific shortcuts if you don't want to use global ones; just rewrite the scripts accordingly (the free Lite version has a 10-shortcut limit, so the CamiScripts plug-in for Camino will come in handy here).

[robg adds: I tested the Camino script with Butler, and it worked fine -- with Safari open, pressing my defined hotkey loaded the current URL into Camino.]

Comments (12)


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