1

I need to create a script that I can bind to a keyboard shortcut. The script opens an instance of mozilla if it is not open and if is it minimizes or maximises the window. Kind of like drop down terminal.

The script is working when I open it from a desktop shortcut but if I open the script directly by clicking on it or macro it to a keyboard shortcut it doesn't work as expected. It only opens mozilla but doesn't minimize or maximize

#!/bin/bash

touch memory.txt
myvar="$( xdotool search Mozilla )"
status="$(grep [0-1] memory.txt)"
run=0

if [ $(grep -c [0-9] <<< $myvar) -gt 0 ]
then
    pwd
else
    (firefox &)
fi

if [ $status = 0 ]
then
    $(xdotool windowminimize $myvar)
    echo -n "1" > memory.txt
    run=1   
fi

if [ $status = 1 ] && [ $run -eq 0 ]
then
    $(wmctrl -ia $myvar)
    echo -n "0" > memory.txt
fi
exit
1
  • What Desktop Environment are you using?
  • How are you defining the keybinding? Through a script or gui or cli tool?

Regarding your script, be sure to define the FULL PATH of your script in the shortcut manager. Do not use ~ because it will not be interpreted by the graphical shell. Also, be sure to set the script to executable.

Here is a script that I use to accomplish the same thing you are after: (I use it for a different application, and so I changed the code below to represent "firefox" in order to match your question.) Note: I am using GNOME 3.28.2 and xdotool 3.20160805.1

#!/bin/bash
wid=$(xdotool search --onlyvisible --name 'firefox')
if test "$wid" = "" ; then exec firefox &
fi
actual=$(xdotool getactivewindow)
if test $wid = $actual ; then xdotool windowminimize $wid
else xdotool windowactivate $wid
fi
##EOF
  • I'm using the newest version of xubuntu. I'm accessing the script through the default keyboard manager. For me your code only slightly works it open mozilla and minimizes it but when accessed the third time it doesn't maximize it creates a new instance. I believe my code works it just doesn't like being accessed through the keybind or when executed but for some reason a desktop shortcut to it works. Thanks for the fast response. – Carter Apas Aug 10 '18 at 2:06
  • Check to see that xdotool is returning a valid ID and storing to the variable $wid. If there is no ID, then the "test" will fail and a new instance of the application will open as you described. – Eminent Aug 10 '18 at 22:54
  • @CarterApas Regarding your script, be sure to define the FULL PATH of your script in the shortcut manager. Do not use "~" because it will not be interpreted by the graphical shell. – Eminent Aug 10 '18 at 23:15
  • I think the problem with your script is in the first line. It only searches for visible firefox applications. If an application is not visible, which it will be when minimized, it will execute a firefox instance. The full path thing didn't work. Any ideas for the desktop shortcut issue apart from those pointed out? – Carter Apas Aug 13 '18 at 3:40
  • I ended up getting it to work by making a bash script that opened the first script, and then linking with keyboard commands Thanks for help – Carter Apas Aug 14 '18 at 2:11

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.