7

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

2
  • @Powertieke: I am using Chrome. But this should go into an application which I want to sell to other users and I doubt that I could just tell them "please use Chrome or Safari instead of Firefox". Commented Mar 15, 2011 at 11:56
  • Maybe you can get the python webbrowser module to fire off an inline javascript which could talk back to the script using google gears or something? Commented Mar 15, 2011 at 12:55

7 Answers 7

8

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

Sign up to request clarification or add additional context in comments.

Comments

5

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.

2 Comments

This is a weird one. Under normal circumstances, I get the same error and result with Firefox v90 under Sierra. However, when VoiceOver is enabled the url is returned. I can't support the answer though as this prerequisite is a non-standard condition for most users. As it stands this isn't a good workaround.
Sorry, only now realized that I had an app which enabled VoiceOver support in FF. Added a section about how to enable it manually, without having VoiceOver enabled globally. Hope it helps!
3
tell application "Firefox" to activate

tell application "System Events"

keystroke "l" using {command down}

keystroke "c" using {command down}

end tell

2 Comments

"... ugly hacks which are no solution for me (sending key presses to copy the URL to clipboard)"
This solution has been repeated all over the 'net, and at least on current fast Apple hardware suffers from a nearly 100% repeatable "race condition" where FF won't have activated before System Events does its thing. It works to stick a big fat delay (2 sec or more, in my informal trials) in between the two tells, but that piles hack upon hack. Note: the delay statement needed is literally "delay 2", for those unfamiliar with AppleScript.
2

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

Comments

1

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.

6 Comments

What do you mean by GUI scripting? I thought that AppleScript is also mostly made for GUI scripting.
GUI Scripting is an aspect of Applescript which allows for the direct maninpulation of controls, like buttons, checkboxes, and the like.
How can this be done for Firefox? And doesn't it require that the application uses Cocoa GUI elements? Because I think FF doesn't use those.
I don't do GUI scripting myself as I find it problematic and too easily prone to error, but there is an intro here: macosxautomation.com/applescript/uiscripting/index.html Firefox should support this by way of Mac OS X, but I can't tell you definitively either way.
I tried a bit like that to find the text box. For other applications, I was able to get a list of all GUI elements (buttons, text boxes, etc.). But for Firefox I was not. So I guess this is not possible.
|
1

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.

2 Comments

This seems to be the only viable option. System events does not see any ui elements below the main window space... I can honestly say that this is the worst applescript support ever.
Well, this is absolutely no option for me. I don't want to invalidate users clipboard. And I want to check for the current URL about every 10 seconds.
1

After some experimentation, I found a solution that works on modern macOS:

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

Based on an answer by @0xZ3RR0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.