Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
767 views
in Technique[技术] by (71.8m points)

firefox - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...