问题
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.
回答1:
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
回答2:
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
回答3:
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
回答4:
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
回答5:
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
回答6:
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.
回答7:
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
回答8:
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
回答9:
OmniWeb (tested on version 5.8):
tell application "OmniWeb"
set myInfo to GetWindowInfo
return item 1 of myInfo
end tell
回答10:
Opera (tested on versions 9.21 and 9.62):
tell application "Opera"
return URL of front document as string
end tell
回答11:
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.
来源:https://stackoverflow.com/questions/263741/using-applescript-to-grab-the-url-from-the-frontmost-window-in-web-browsers-the