Question

I'm using Selenium in order to control multiple Firefox sessions at the same time.

In this context, the OS handles each Firefox window as a different application.

I want to build a workflow that'll allow me to close all the recent windows of Firefox (leaving the "oldest" one working), but I couldn't find any way to sort them by their opening date.

Any help would be appreciated!

Was it helpful?

Solution 3

With some help from this answer I was able to print the IDs and the names of all the processes that currently run.

I then ran a few tests and these are my insights:

  1. The processes are sorted by most recent.
  2. The Firefox processes that Selenium opens are named "firefox-bin", as opposed to the usual "firefox" process/es.
  3. Hence the IDs' order matches the names' order, so it allows to cross between the lists and identify the windows that Selenium has opened.

The code I used is:

tell application "System Events" to get the name of every process whose background only is false
set _name to result
tell application "System Events" to get the id of every process whose background only is false
set _id to result

set AppleScript's text item delimiters to space
do shell script ({"echo", _name & " " & _id, "| tr ' ' '\\n'", "| sort -n"} as string)`

It's not optimal, but I just needed to get the right IDs, so this test was enough for me to move on.

OTHER TIPS

Try to access window menu of Firefox, the oldest window at top of the list

I have no experience with Selenium, but I wonder if you could use an AppleScript (which Alfred can happily make use of) to get the window id's of each Firefox window. Window id’s are normally assigned incrementally, so a recent window will have an id of greater numeric value than those that came before it.

    tell application "Firefox" to get every window

should return a list of all windows referenced by their id numbers, e.g.

    {window id 59 of application "Firefox", window id 2408 of application "Firefox", window id 61 of application "Firefox", window id 60 of application "Firefox"}

Of course, I wonder how running multiple, separate instances of Firefox will affect this, as I can't predict whether AppleScript will send the get windows command to all Firefox instances or just one (and, if so, which one?).

If that line works as I want it to, then you can pick out the one with the smallest id and close the others. Here's one way to do that:

    tell application "Firefox" to get the id of every window

    set W to result

    set AppleScript's text item delimiters to space
    do shell script ({"echo", W, "| tr ' ' '\\n'", "| sort -n"} as string)
        --> Outputs lines sorted from smallest to greatest value

    set W to first paragraph of result

    tell application "Firefox" to close (windows whose id > W)
Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top