Skip to content

Instantly share code, notes, and snippets.

@mkhl
Created February 7, 2009 14:30
Show Gist options
  • Save mkhl/59891 to your computer and use it in GitHub Desktop.
Save mkhl/59891 to your computer and use it in GitHub Desktop.
Insert the Current URL of the default browser. For use with ThisService.
#!/usr/bin/env ruby -rubygems
# Unicode considerations:
# Set $KCODE to 'u'. This makes STDIN and STDOUT both act as containing UTF-8.
$KCODE = 'u'
# Since any Ruby version before 1.9 doesn't much care for Unicode,
# patch in a new String#utf8_length method that returns the correct length
# for UTF-8 strings.
UNICODE_COMPETENT = ((RUBY_VERSION)[0..2].to_f > 1.8)
unless UNICODE_COMPETENT # lower than 1.9
class String
def utf8_length
i = 0
i = self.scan(/(.)/).length
i
end
end
else # 1.9 and above
class String
alias_method :utf8_length, :length
end
end
require 'appscript'
browser = `perl -MMac::InternetConfig -le 'print +(GetICHelper "http")[1]'`.chomp
case browser
when 'Safari', 'WebKit'
print Appscript.app(browser).documents.first.URL.get
when 'Camino'
print Appscript.app(browser).windows.first.tabs.first.URL.get
when 'Firefox'
print Appscript.app(browser).windows.first.properties_.get[AEM::AEType.new('curl')]
else
print "Couldn't find default browser..."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment