rein rein - 1 month ago 15
AppleScript Question

Closing specific tab in FireFox using AppleScript

I need to script the closing of tabs with specific titles in FireFox. Is there a FireFox equivalent to the AppleScript examples below?

Close tabs in Safari:

tell application "Safari"
delete (every tab of every window where its name contains "[done]")
end tell


Close tabs in Chrome:

tell application "Google Chrome"
delete (every tab of every window where its title contains "[done]")
end tell

Answer

Since Firefox doesn't have any AppleScript support you'll need to rely on UI scripting. Of course, Firefox's non-standard UI doesn't help either but it's still possible. Try this:

tell application "System Events" to tell process "firefox"
    set frontmost to true
    repeat with w from 1 to count of windows
        perform action "AXRaise" of window w
        set startTab to name of window 1
        repeat
            if name of window 1 contains "[done]" then
                keystroke "w" using command down
            else
                keystroke "}" using command down
            end if
            delay 0.2
            if name of window 1 is startTab then exit repeat
        end repeat
    end repeat
end tell
Comments