Skip to content

Instantly share code, notes, and snippets.

@kch
Created January 28, 2011 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kch/799710 to your computer and use it in GitHub Desktop.
Save kch/799710 to your computer and use it in GitHub Desktop.
browser function used in my appscripts to target Safari or WebKit dynamically
#!/usr/bin/env ruby
# encoding: UTF-8
require 'appscript'
VALID_BROWSERS = %w[ WebKit Safari ]
# Used in my appscripts to target Safari or WebKit dynamically.
# Returns an Appscript::Application for one of these browsers following this priority:
# 1. is frontmost
# 2. is running and default
# 3. is running
# 4. is default
def browser
processes = Appscript::app("System Events").processes
target, = VALID_BROWSERS & processes[Appscript.its.frontmost.eq(true)].short_name.get
target ||= begin
default_browser = VALID_BROWSERS & [%x(VERSIONER_PERL_PREFER_32_BIT=yes /usr/bin/perl -MMac::InternetConfig -le 'print +(GetICHelper "http")[1]').chomp]
running_browsers = VALID_BROWSERS & processes.short_name.get
(running_browsers & default_browser)[0] || running_browsers[0] || default_browser[0]
end
Appscript::app(target) if target
end
@kch
Copy link
Author

kch commented Jan 28, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment