Browser Detection via rb-appscript

February 1st, 2009  |  Published in ruby

Gruber has a bit of Applescript for detecting browsers similar to something I did in Ruby early last year.

His is for differentiating between WebKit and Safari, mine’s aimed at Safari, Camino or Firefox (because I’m a fickle, futzing bastard). His goes a step further and supplies the user’s default browser if neither Safari or WebKit is frontmost. Mine assumes it’s being triggered from FastScripts (or the generic script menu), and doesn’t really need to know the default browser because I just wanted to grab the URL of the active browser and use it elsewhere.

The Firefox piece, which was hideous, is broken now, per Luis’ plaint (but it’s being worked on).

#!/usr/bin/ruby # sniff a browser # v0.1 – 2/18/08 require ‘rubygems’ require ‘appscript’ include Appscript require ‘CDEV’ require ‘osax’ include OSAX frontmost = app(‘System Events’).processes[its.frontmost.eq(true)].title.get.to_s browser = app(frontmost) if frontmost == ‘Safari’ url = browser.documents[0].URL.get elsif frontmost == ‘Camino’ url =[1].tabs[1].URL.get elsif frontmost == ‘OmniWeb’ url = browser.browsers[0].address.get elsif frontmost.match(/Firefox|Minefield/) url = \`osascript\<\END tell application "Firefox" set myFirefox to properties of front window as list set theUrl to item 3 of myFirefox return theUrl end tell END` else osax.display_dialog("A browser is not the frontmost app!") end cdev = osax.set_the_clipboard_to(cdev.pracnetnews) [/sourcecode]

Leave a Response

© Michael Hall, licensed under a Creative Commons Attribution-ShareAlike 3.0 United States license.