Ubuntu – Help with bash script for Docky

bashdockygconf

I'm going to assign this script to a keyboard shortcut so that I can toggle Autohide quickly for Docky.

I'm going to make a second script that switches off Autohide again, and assign a different keyboard shortcut to that.

The reason for closing and relaunching Docky is because Docky doesn't refresh the configuration's settings otherwise. I've written a sudo command, and I suspect that it won't work.

I want to:

  1. Do it without any sudo command
  2. If it's possible (not important), have one unified script that just toggles between "None" and "Intellihide", so that I don't need to have two seperate keys.
  3. If possible, have Docky refresh the configuration's values without having to exit and launch again.
#!/bin/bash 
gconftool-2 -t string -s /apps/docky-2/Docky/Interface/DockPreferences/Dock1/Autohide "Intellihide" & 
sudo killall dockey & 
docky

Best Answer

Well, first of all, you are killing the wrong program. Your script has sudo killall dockey & but you want to kill docky, not dockey. That said, there is absolutely no reason for sudo, docky has been started by your user so you can kill it without any special privileges. You also don't need to send it to the background (that's what the & does).

You haven't explained why your script doesn't work but what is probably happening is that you run it and you see nothing happening (please always explain what the symptoms are when asking questions). This is because you are sending the sudo ... command to the background with &. sudo expects a password:

$ sudo ls
[sudo] password for terdon: 

So, when you run it, it will provide a prompt asking for a password. Since you are running it in the background, you will never see that prompt and the script will be stuck, waiting for you to answer.

I don't use docky and I have no idea if you can do this without restarting it but restarting seems to be the simplest option. Just change your script to:

#!/bin/bash

gconftool-2 --get  /apps/docky-2/Docky/Interface/DockPreferences/Dock1/Autohide | grep -q None &&
    gconftool-2 -t string -s /apps/docky-2/Docky/Interface/DockPreferences/Dock1/Autohide "Intellihide" ||
    gconftool-2 -t string -s /apps/docky-2/Docky/Interface/DockPreferences/Dock1/Autohide "None"    

killall docky && docky

The && means AND, in other words "run the next command only if the previous one was successful". The || (OR) is the opposite, "run the next command if the previous one failed. So, the script above will first query gconf for the current state of Autohide. The grep will only be successful if that matches None. If so, it is set to Intellihide and if not, it is set to None

Note that && is not the same as &. The & sends commands to the background as described above and is not needed unless you want to send something to the background. For more details on the various shell operators, see here.

Related Question