How To Add A Dynamic Trash Icon To Ubuntu Dock (Gnome Shell)

Trash icon gnome shell dock

As you might be aware, Gnome Shell doesn't offer an option to add a trash icon to its Dash. Dock extensions like Ubuntu Dock, Dock to Panel or Dash to Panel don't come with options for this either, so Kerry, an Ubuntu Forums user, created a script that makes it possible to add a dynamic Trash icon to the Ubuntu Dock (Gnome Shell).

The script should work with most docks, and not just the Ubuntu Dock (Dash to Dock / Dash to Panel, etc.), and it uses a 30 second timer to check if the trash can is empty or not and change the icon accordingly. You can also empty the trash from trash icon context (right click) menu.

How To Add A Trash icon to Ubuntu Dock in Gnome Shell


1. Download the script from Ubuntu Forums (Trash.zip from post #16) in your home folder and extract it there.

2. Run the script to install all the required files. Make sure the script and two trash icons are in a folder called "Trash" placed in your home directory.

As a general rule, I recommend checking out the code used in this script and any other scripts you run on your system, and only run them if you have at least some basic understanding of what they do.

To automatically create the script autostart file and its application desktop file (so it shows up in the Activities), open a terminal, navigate to ~/Trash (that's where the script contents are extracted) and run the script with the -s. You can do this by running the following commands:

cd ~/Trash
chmod +x trash.sh
./trash.sh -s

3. Logout and login

Don't simply restart the session, as that won't be enough. The startup script needs to run, so you'll need to logout and log back in.

4. Search for trash in the Gnome Shell Activities and select Add to Favorites to add it to Ubuntu Dock / Dash to Dock / Dash to Panel, or whatever dock you're using. The trash bin icon should change to red if the trash is full or green if the trash is empty. Give it some time though, as it only checks if the trash is empty or full every 30 seconds.

Tweaking the Trash icon script


Tweak 1: hide the Trash folder in your home directory

The script needs the ~/Trash folder to exist or else it won't work (the paths are hardcoded). If this bothers you, the ~/Trash folder can easily be hidden so it's not displayed by the file manager. To do this, create a file called .hidden (it starts with a dot!) in your home folder and in this file, add Trash. This way, the file manager will treat ~/Trash as a hidden folder, only displaying it when you choose to display hidden files and folders.

Tweak 2: change the hardcoded trash icons with your current icon theme trash icons

Trash icon Gnome Shell dock

The script uses custom, hardcoded icons by default. If you want the trashcan icon from the dock to use your icon theme, open the trash.sh script with a text editor such as Gedit, and replace:

  • $(xdg-user-dir)/Trash/user-trash-full.svg with user-trash-full
and
  • $(xdg-user-dir)/Trash/user-trash.svg with user-trash

And save the file. In 30 seconds or less (that's how often the script checks if the trash is empty or full), the dock trash icon should use your current icon theme.


Thanks to Kerry for the script!

No comments: