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

有些话、适合烂在心里 提交于 2019-11-28 19:36:22

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

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

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

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

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

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.

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

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

OmniWeb (tested on version 5.8):

tell application "OmniWeb"
    set myInfo to GetWindowInfo
    return item 1 of myInfo
end tell

Opera (tested on versions 9.21 and 9.62):

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!