I’m trying to write an Applescript to select a specific Firefox window (either by window title containing certain words or URL), then force it to reload.
I’ve tried referring to the windows as ‘1’ and ‘2’ but which is which seems to depend on when each window was last activated rather than the order in which they opened.
I’ve also tried using GUI scripting to select the window I want via Firefox’s “Window” menu. Unfortunately when the window I want has failed to load the name changes and I can no longer address it via GUI scripting (using either the full window title or abbreviated as displayed in the “Window” menu fails to work).
I could also settle for sending a reload to each Firefox window, if that’s easier to accomplish.
Yes - I’ve found this from searching the forum for Firefox in general. I thought there might be some other clever way to locate the right window, but as yet I’ve found nothing.
I’ve been thinking further about the ‘Firefox issue’ and wondering if the following would work. Would it be possible to iterate through the open Firefox windows, checking some property of the page until the target page is found, then restore it and send it a ‘reload’ via system events?
I can picture the following psuedo-code but i’m not sure how to write it:
set targetURL=“http://www.google.com”
set n to 0
repeat while not (currentURL = targetURL)
tell application “Firefox”
set n to n+1
activate window n
set currentURL to {not sure how to get the current URL}
end tell
end repeat
{restore the window if minimised - again not sure how to do this via applescript}
{followed by the system events bit to send the reload}
I also need something added to manage the error generated once all open Firefox windows have been checked, but again not sure how to accomplish this.
Using GUI Scripting, you can bring a named Firefox window to the front by setting the value of its attribute “AXMain” to ‘true’:
tell application "System Events"
tell application process "firefox-bin"
set frontmost to true
set value of attribute "AXMain" of (first window whose name contains "Google") to true
end tell
end tell
One way of identifying a window’s URL is to bring the window to the front, select the text in the URL field, and copy it to the clipboard. Then see what’s on the clipboard. It’s a bit clunky, but it seems to work:
set targetURL to "http://www.google.com/"
tell application "System Events"
tell application process "firefox-bin"
set frontmost to true
set windowNames to name of windows whose subrole is "AXStandardWindow"
repeat with thisName in reverse of windowNames
-- Don't act with non-browser windows.
if not ((thisName is in {"Downloads", "Error Console", "Add-ons"}) or (thisName begins with "Source of:")) then
-- Bring this window to the front.
set value of attribute "AXMain" of window thisName to true
-- Set the clipboard to a neutral value.
set the clipboard to ""
-- Select and copy the URL field. This relies on the Navigation Bar being visible.
keystroke "lc" using command down
-- Delay until the clipboard contents change.
repeat while ((the clipboard) is "")
delay 0.2
end repeat
-- Compare the new contents with the target URL. If they're the same, reload the page and finish.
if ((the clipboard) is targetURL) then
keystroke "r" using command down
exit repeat
end if
end if
end repeat
end tell
end tell
Actually I think that approach will work with one small modification. As the URL I’m targetting contains a random session ID, I need to select a substring of it (say the leftmost 20 characters for example). I could do this in SQL easily, but I’ve no idea how to achieve it in Applescript. Any ideas?
Got a bit further - found the Applescript reference and worked out how to check if the URL ‘contains’ or ‘starts with’ the target. Unfortunately I’ve hit another issue - the address bar is hidden. :rolleyes:
Do you think this would work using window name rather than URL?
set targetName to "Problem"
tell application "System Events"
tell application process "firefox-bin"
set frontmost to true
set windowNames to name of windows whose subrole is "AXStandardWindow"
repeat with thisName in reverse of windowNames
-- Don't act with non-browser windows.
if not ((thisName is in {"Downloads", "Error Console", "Add-ons"}) or (thisName begins with "Source of:")) then
if (thisName contains targetName) then
-- Bring this window to the front.
set value of attribute "AXMain" of window thisName to true
keystroke "r" using command down
exit repeat
end if
end if
end repeat
end tell
end tell
I think this may have the added benefit of refreshing all windows that failed to load when the network wasn’t connected, but I haven’t tested that yet.
You’ll need to remove the ‘exit repeat’ line if you want to refresh all failed loads. As it stands, the script will stop after refreshing first one it finds.
It’ll also refresh forum pages with titles like “Problem scripting Firefox”.
Thanks - I thought that might be the case but I’m happy for it to reload just the one window. I’ve managed to narrow it further as Firefox seems to change the window name (conveniently) to “{URL} - Problem loading page.” I’m not sure if it does this for all sites, but it works for the one I need.
This, along with Wifiscriptor, resolves one of my greatest annoyances with the Mac. After waking from sleep I find that several things time out (video streams, web pages, mail) while waiting for the wireless network to reconnect. Now as soon as I have an IP on the network it sorts itself out.
Don Crockett has pointed out this method today on the AS-Users mailing list:
tell application "Firefox" to get «class curl» of window 1
It returns the URL of the current tab in the frontmost visible window in Firefox, which saves faffing around with the clipboard and even works when the navigation bar’s hidden. Tom (this thread’s OP) changed his approach in the end, but I thought I’d just mention «class curl» for completeness, since checking the URL was one of the original ideas.