Created
January 28, 2011 02:24
-
-
Save kch/799710 to your computer and use it in GitHub Desktop.
browser function used in my appscripts to target Safari or WebKit dynamically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is part of http://stdout.caiochassot.com/post/2969463643/writing-applescripts-that-dynamically-target-either