Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using AppleScript to grab the URL from the frontmost window in web browsers: The definitive list

I built a [widget][1] that grabs the URL from the frontmost window in Safari, then allows you to shorten it using the tr.im API. Works sweet as.

I want to make this more flexible, so am investigating how to grab an URL from other browsers. Here's the AppleScript that works in Safari:

tell application "Safari"
    return URL of front document as string
end tell

After some digging, I determined that the following might work for Firefox (though one person has told me it doesn't work for him, possibly a conflict with some extension?):

tell application "Firefox"
    set myFirefox to properties of front window as list
    return item 3 of myFirefox
end tell

Note: The above is an example of a less-than-best practice, relying on the position of list items. See below for a better solution for Firefox.

What I'd like to do is build a list here of the definitive equivalents for every modern browser on the Mac: Opera, Camino, Flock, etc.

Update: In my research on the subject, I came across a helpful thread on MacOSXHints.com. Most of my answers below are based on that discussion.

Update 2: I've incorporated the AppleScript on this page into the [widget][1]. It seems to be working swell.

like image 858
Andrew Hedges Avatar asked Nov 04 '08 22:11

Andrew Hedges


1 Answers

Activate UI scripting and run the code below. You will then have the URL in the clipboard and you can paste it.

tell application "Firefox" to activate
tell application "System Events"
    keystroke "l" using command down
    keystroke "c" using command down
end tell
like image 85
Brian Avatar answered Sep 20 '22 14:09

Brian