2

I have this script that runs via a keyboard shortcut (and toggles the Stylus extension stylesheet on/off***). It is specifically for maximized windows so the xdotool values won't change:

#!/bin/bash

xgg="$(xdotool getactivewindow getwindowname)"

if [[ "$xgg" == *" - Mozilla Firefox" ]]
  then
    xdotool mousemove --sync 18 54 click 1 
    sleep 0.2
    xdotool mousemove --sync 134 85 click 1
    sleep 0.2
    xdotool mousemove --sync 1365 85 click 1
    sleep 0.2
    xdotool mousemove --sync 683 384
fi

It checks if the window in focus is Firefox because of *" - Mozilla Firefox", which is how titles of Firefox windows end, and, if it matches, it performs the subsequent xdotool commands.

I now would like the xdotool commands to run even if the window is a Private Browsing instance of Firefox.

If I have both a regular Firefox window and a private browsing window open, wmctrl -l shows this:

$ wmctrl -l
0x0260000b -1    N/A Desktop — Plasma
0x02600016 -1    N/A Plasma
0x03e00018  0 kububb Newest Questions - Ask Ubuntu - Mozilla Firefox
0x03e001cd  0 kububb Newest Questions - Ask Ubuntu - Mozilla Firefox (Private Browsing)
0x05a00006  0    N/A bash: dkb — Konsole
$ 

What regex is required for the script to recognize both - Mozilla Firefox and - Mozilla Firefox (Private Browsing) at the end of the windows' titles?

*** I have the icon for Stylus to the left of the address bar. When the icon is clicked once, a dropdown appears.The second click (in a specific area of the dropdown) toggles the stylesheet on/off.

7
  • 1
    *? Or you can also use logical OR. – Kulfy Nov 5 '19 at 11:27
  • @pLumo, no, only maximized. I'll edit the question accordingly. – DK Bose Nov 5 '19 at 12:16
  • 1
    Not an answer to your question, but Stylus can be toggled via shortcut you can set up in the options ... – pLumo Nov 5 '19 at 12:29
  • The suggestion by @Kulfy works and would be accepted as if posted as an answer. – DK Bose Nov 5 '19 at 12:35
  • @pLumo but yours works as well! I didn't know about it. And it's independent of window size. – DK Bose Nov 5 '19 at 12:38
3

You can use asterisk (*) wildcard. * can represent any number of characters (including zero, in other words, zero or more characters). Therefore, if would look like:

if [[ "$xgg" == *" - Mozilla Firefox"* ]]

Or since there are only two possibilities for Mozilla Firefox, i.e., Mozilla Firefox and Mozilla Firefox (Private Browsing), you can use logical OR in if to be more specific.

if [[ "$xgg" = *" - Mozilla Firefox" ]] || [[ "$xgg" = *" - Mozilla Firefox (Private Browsing)" ]]

Or as per DK Bose's comment:

if [[ "$xgg" == *?(" - Mozilla Firefox"|" - Mozilla Firefox (Private Browsing)") ]]
2
  • Taking you hint in a comment, I went with if [[ "$xgg" == *?(" - Mozilla Firefox"|" - Mozilla Firefox (Private Browsing)") ]]. That works. Re. your first code, please see my comment to terdon's answer re. the * at the end. If the code I posted works for you, you can put it in your answer. – DK Bose Nov 5 '19 at 13:07
  • @DKBose Understood and done. – Kulfy Nov 5 '19 at 13:25
3

Just use a glob (nitpick, but == takes globs not regular expressions) that allows strings after Firefox:

#!/bin/bash

xgg="$(xdotool getactivewindow getwindowname)"
if [[ "$xgg" = *"Mozilla Firefox"* ]]
then
    xdotool mousemove --sync 18 54 click 1 
    sleep 0.2
    xdotool mousemove --sync 134 85 click 1
    sleep 0.2
    xdotool mousemove --sync 1365 85 click 1
    sleep 0.2
    xdotool mousemove --sync 683 384
fi

Alternatively, since you're using [[ anyway, you can use the regex match:

#!/bin/bash

xgg="$(xdotool getactivewindow getwindowname)"
if [[ "$xgg" =~ "Mozilla Firefox" ]]
then
    xdotool mousemove --sync 18 54 click 1 
    sleep 0.2
    xdotool mousemove --sync 134 85 click 1
    sleep 0.2
    xdotool mousemove --sync 1365 85 click 1
    sleep 0.2
    xdotool mousemove --sync 683 384
fi
2
  • The code I posted ensures the string is at the end of the window title. This makes the chance, however slim, of the shortcut being mistakenly used on a non-Firefox window slimmer. That's why I didn't go with the terminal *. – DK Bose Nov 5 '19 at 13:04
  • 1
    @DKBose then go with Kulfy's suggestion of ||. – terdon Nov 5 '19 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.