How To Add A Show Desktop Icon To Ubuntu Dock / Dash To Dock

Show Desktop icon Gnome Shell dock

If you're a Gnome Shell user and you're missing having a Show Desktop icon on your dock to quickly show / hide all open windows, here's a script to achieve this with the Ubuntu Dock, Dash to Dock, or any dock you may be using.

Dash to Panel users don't need to use a script. The Dash To Panel Gnome Shell extension has an option to enable a Windows-like Show Desktop button at the right-hand side end of the dock. This can be done from the Dash to Panel Settings > Behavior > Show Desktop button.

For docks like the Ubuntu Dock, Dash To Dock or others, here's a script that you can use to get a Show Desktop button on your Gnome Shell dock.

This article includes 2 scripts from which you'll have to choose one. The first script, which we'll call Script A, hides and shows visible windows only, so if some windows were minimized, they won't be restored when using the Show Desktop icon. Script B restores all windows, including those that were previously minimized, so even if some windows were previously minimized and they weren't hidden by clicking on the Show Desktop icon, they are restored when clicking the Show Desktop icon for the second time, along with the other windows.

Let's get started.

1. Install wmctrl

We'll use wmctrl to show and hide open windows, so you need to install this tool. In Debian / Ubuntu you can install it using this command:

sudo apt install wmctrl

2. Create the script and make it executable

Run the following commands to create a file called show-desktop in /usr/local/bin and open this file as root, with a text editor (Gedit):

sudo touch /usr/local/bin/show-desktop
gedit admin:///usr/local/bin/show-desktop

After you type the root password, the /usr/local/bin/show-desktop file should open in Gedit. In this file, paste the contents of one of the two following scripts (only use one - choose the one that suits your needs):

  • Script A (restores only windows that were previously visible, ignores previously minimized windows):
#!/bin/bash
status="$(wmctrl -m | grep "showing the desktop" | sed -r 's/(.*)(ON|OFF)/\2/g')"

if [ $status == "ON" ]; then
    wmctrl -k off
else
    wmctrl -k on
fi

  • Script B (restores all windows, including minimized windows):
#!/bin/sh
#Record Open Windows and Minimize Them
 open_windows=$(wmctrl -l | cut -f1 -d " ")

 if wmctrl -m | grep -e "mode: OFF" -e "mode: N/A" ; then
  wmctrl -k on 
 fi
#Restore Minimized Windows (in the order in which they were opened - newest on top)*
 if wmctrl -m | grep "mode: ON" ; then
  for i in $open_windows
   do 
    wmctrl -i -R "$i"
   done
 fi

Save the file and make it executable using the following command:

sudo chmod +x /usr/local/bin/show-desktop

3. Create a Show Desktop desktop file in ~/.local/share/applications/

Show Desktop icon Gnome Shell Ubuntu Dock

To get the script to show up on docks / panels, we'll create a desktop file for it. To create a file called show-desktop.desktop in ~/.local/share/applications/ and open it with Gedit, use:

gedit ~/.local/share/applications/show-desktop.desktop

In this file paste the following:

[Desktop Entry]
Type=Application
Name=Show Desktop
Icon=desktop
Exec=show-desktop

And save the file.

4. Now search for Show Desktop among your applications (Activities / Applications button), right click the Show Desktop icon and select Add to Favorites.

Add Show Desktop favorites Gnome Shell

After this, the Show Desktop icon should show up on your Dash to Dock / Ubuntu Dock. For other docks, you may need to drag and drop the Show Desktop icon from ~/.local/share/applications/ onto the dock to pin it.

You can now try your new Gnome Shell Show Desktop icon from your dock.

The icon may not update until you logout and log back in.


Scripts credits: Script A | Script B