Albert
Albert

Reputation: 68110

MacOSX or AppleScript: get current URL from Firefox

How can I get the current URL from Firefox (3 or 4)? All solutions I have found so far either don't work (e.g. those with "class curl") or are ugly hacks which are no solution for me (sending key presses to copy the URL to clipboard).

Upvotes: 5

Views: 5133

Answers (6)

0xZ3RR0
0xZ3RR0

Reputation: 596

Since Firefox 87 you can use native AppleScript GUI scripting to get the current URL. That's because Firefox now has support for VoiceOver.

First enable Firefox support for VoiceOver by going to about:config and setting the accessibility.force_disabled property to -1. Note that VoiceOver doesn't have to be enabled, only the support in Firefox. (Extra info at [2].)

After that, you can use:

tell application "System Events" to get value of UI element 1 of combo box 1 of toolbar "Navigation" of first group of front window of application process "Firefox"

[1]: Enabling VoiceOver support makes Firefox expose the internal structure of its window for GUI scripting.
[2]: For extra info and a non-permanent option (toggling AXEnhancedUserInterface via AppleScript) look at this bug report.

Upvotes: 3

Albert
Albert

Reputation: 68110

Well, I have one solution now: I am reading it out of the current session store of Firefox. This works but it is always up to 10 seconds out-of-date.

Here is the code: https://github.com/albertz/foreground_app_info/blob/master/mac/app-scripts/url%20of%20Firefox.py

Upvotes: 7

clt60
clt60

Reputation: 63892

Easy, if you install two components:

If you have installed both, the following command

perl -MWWW::Mechanize::Firefox -E 'say WWW::Mechanize::Firefox->new(tab=>'current')->base()'

will print the topmost active firefox url, in like:

http://stackoverflow.com/questions/5296995/macosx-or-applescript-get-current-url-from-firefox

Upvotes: 2

JAC
JAC

Reputation: 21

tell application "Firefox" to activate

tell application "System Events"

keystroke "l" using {command down}

keystroke "c" using {command down}

end tell

Upvotes: 2

kindall
kindall

Reputation: 184101

Firefox 3's AppleScript implementation is lame enough that you have to resort to copying the URL out of the Location field, as shown in the last post is this thread.

Upvotes: 1

Philip Regan
Philip Regan

Reputation: 5045

A window's URL is not exposed via the Applescript API; not even a window's tabs. The only way you'll be able to get it is through GUI scripting.

Firefox's Applescript implementation is simply awful, and I don't know why they even bother.

Upvotes: 1

Related Questions