views:

3036

answers:

9

I built a widget 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. It seems to be working swell.

A: 

Opera (tested on versions 9.21 and 9.62):

tell application "Opera"
    return URL of front document as string
end tell
Andrew Hedges
A: 

Firefox (tested on versions 2.0.0.14 and 3.0.1):

tell application "Firefox"
    set myURL to «class curl» of window 1
    return myURL
end tell
Andrew Hedges
A: 

Camino (tested on version 1.6.4):

tell application "Camino"
    set p to properties of front tab of front window
    return |currentURI| of p as string
end tell
Andrew Hedges
This script always returns the URL for the first tab in Camino, regardless of which tab has focus. Is there a better way?
Andrew Hedges
A: 

Flock (tested on version 2.0):

tell application "Flock"
    set p to properties of front window as list
    return item 3 of p
end tell

This relies on the position of the list item, but as far as I can tell, this is the only way to get at this value. The property is named address which, though Apple's documentation doesn't say so, appears to be a reserved word in AppleScript.

Andrew Hedges
A: 

OmniWeb (tested on version 5.8):

tell application "OmniWeb"
    set myInfo to GetWindowInfo
    return item 1 of myInfo
end tell
Andrew Hedges
+1  A: 

There is currently a bug in Firefox 3.03, that will hide from AppleScript all of the window properties including «class curl», if you have used a statement like the following before :

tell application "Firefox" to activate

or

tell application "Firefox"
 if (front window) exists then do_something()
end tell

the work around is to use the following code instead :

tell application "System Events"
 tell process "Firefox"
  set frontmost to true
  set xsist to (front window) exists
  (* keep xsist value to test later on *)
 end tell
end tell

Note : the window's properties will remain unavailable until next relaunch of Firefox

That explains why that wasn't working for my friend. Thanks!
Andrew Hedges
thanks for the comment ! I am glad to know it could help, and that the bug is not due to something specific to my configuration as well
+1  A: 

This is Piero again back with a new id (I lost my cookies while trying to reinstal Firefox !!!).

I just tried Firefox 3.04 nothing changed about appleScript support and relyability. Still the same bug ...

My test and searches over the web, brought me to the conclusion that you cannot access the name of the window, and other properties of the window, such as «class curl», in the same script.

If you are working with the name of the window, and that, suddently, you cannot access it anymore (getting random binary like strings), you have to call this code again :

tell application "Firefox" to activate

using any statement that will generate an error in Firefox will also work just fine, to make window name available again, but restarting your Mac won't change anything !

Once you have done that, as I mentioned before, you cannot access «class curl» anymore, until next Firefox restart ...

writing scripts for Firefox on Macs is really mission impossible !

If you would like AppleScript to be supported on Firefox tell it, and vote for this bug !!!

https://bugzilla.mozilla.org/show_bug.cgi?id=464701

piero
+1  A: 

Camino 1.6 and above:

tell application "Camino"
    return URL of current tab of front browser window as text
end tell

Unlike the earlier answer, this will get the focused tab's URL.

smorgan
Sweet, thanks! Does this work for Firefox as well, do you know?
Andrew Hedges
Nope, our AppleScript implementation is completely separate from Firefox's.
smorgan
A: 

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
Brian