15

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.

Joshua Goldberg
  • 5,059
  • 2
  • 34
  • 39
Andrew Hedges
  • 21,688
  • 16
  • 67
  • 79

11 Answers11

3

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
  • 31
  • 1
3

Google Chrome for Mac has added the AppleScripting method for getting the URL.

Here's the Chromium AppleScript SDK

https://sites.google.com/a/chromium.org/dev/developers/design-documents/applescript

Example from the page linked below:

   tell application "Google Chrome"
     get URL of active tab of window 1
   end tell

More examples here:

http://laclefyoshi.blogspot.com/2010/10/google-chrome-ver.html

Neffster
  • 136
  • 5
2

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
  • 21,688
  • 16
  • 67
  • 79
2

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

  • 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 –  Nov 10 '08 at 20:53
2

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 B
  • 37
  • 6
1

Thanks to Brian above, this is the bullet proof version.

His code asks you to paste the URL, but this one sets the URL to "FrontDocumentURL" which you can then use as a variable in your scripts.

tell application "Firefox" to activate

tell application "System Events"
keystroke "l" using command down
keystroke "c" using command down
end tell

set FrontDocumentURL to the clipboard
Fred
  • 11
  • 1
1

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
  • 20,228
  • 3
  • 47
  • 55
0

Opera (tested on versions 9.21 and 9.62):

tell application "Opera"
    return URL of front document as string
end tell
Andrew Hedges
  • 21,688
  • 16
  • 67
  • 79
0

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
  • 21,688
  • 16
  • 67
  • 79
  • 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 Feb 04 '09 at 20:07
0

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
  • 21,688
  • 16
  • 67
  • 79
0

OmniWeb (tested on version 5.8):

tell application "OmniWeb"
    set myInfo to GetWindowInfo
    return item 1 of myInfo
end tell
Andrew Hedges
  • 21,688
  • 16
  • 67
  • 79