0

I want to open several tabs from a particular domain each with different assets as defined in a bash script. To avoid getting banned from the server, I open tabs with an interval of 5 seconds as follows:

for ((i = 0; i < nWebsite; i = i + 1)); do
    (firefox -new-tabs "${WebsiteString[i]}" > /dev/null 2>&1 &)
    sleep 5
done

But, unfortunately, whenever a new tab is opened, the focus shift to the firefox and due to multiple tabs that I want to open it keeps happening for a long duration.

How can I open every tab in the above for loop minimized?


I tried the following:

for ((i = 0; i < nWebsite; i = i + 1)); do
    (firefox -new-tab "${WebsiteString[i]}" > /dev/null 2>&1 &)
    xdotool getactivewindow windowminimize
    sleep 5
done

But this doesn't solve the problem, as the focus still shifts to the new window, which is then minimized.


Although I asked for Firefox, but it would be great if the solution works for google-chrome as well!

1 Answer 1

0

I think the solution is that you are not saving your current active window ID.

From this page

xdotool getactivewindow > ~/.window_id
for ((i = 0; i < nWebsite; i = i + 1)); do
    (firefox -new-tab "${WebsiteString[i]}" > /dev/null 2>&1 &)
    xdotool windowfocus $(cat ~/.window_id)
    xdotool windowactivate $(cat ~/.window_id)
    sleep 5
done

Of course, that will do it for each iteration of the loop. If you want it just when the loop is done (after 25 seconds) put the xdtools outside the loop, like so:

xdotool getactivewindow > ~/.window_id
for ((i = 0; i < nWebsite; i = i + 1)); do
    (firefox -new-tab "${WebsiteString[i]}" > /dev/null 2>&1 &)
    sleep 5
done
xdotool windowfocus $(cat ~/.window_id)
xdotool windowactivate $(cat ~/.window_id)
4
  • It didn't work! The focus changes from terminal to browser, then back to terminal and this goes on every 5 seconds. Also, does it work in cases when the browser was previously open or not?
    – Porcupine
    Commented Oct 30, 2019 at 21:19
  • Ok, then put the xdtool outside of the loop. I will add an answer. Also, note, I am just trying to solve your focus problem, and I was not working on open every tab minimized.
    – number9
    Commented Oct 31, 2019 at 14:52
  • In the suggested changes, the focus has still shifted to the browser from the terminal, and the focus returns to the terminal only after all URL are loaded. This is not intended, as I don't want to lose focus from terminal.
    – Porcupine
    Commented Oct 31, 2019 at 20:37
  • I do not think, with the current tools built into most window managers under linux, that you can not loose focus for a short time. Remove "sleep 5" and see if that is to your satisfaction, OR, force a fast focus refresh of your terminal, as this post suggests.
    – number9
    Commented Nov 1, 2019 at 12:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .