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).
7 Answers
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
Comments
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
tell application "Firefox" to activate
tell application "System Events"
keystroke "l" using {command down}
keystroke "c" using {command down}
end tell
2 Comments
Easy, if you install two components:
- the
mozreplfirefox extension (github) - and WWW::Mechanize::Firefox perl module
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
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
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
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
webbrowsermodule to fire off an inline javascript which could talk back to the script using google gears or something?