2

I want to make a trash icon for my dock. Dash to Dock does not have a trash icon that I can add, so I was going to make one. I want the Trash full icon when something gets added to the trash, and I want the Trash Empty icon for when there is nothing in the trash. Can somebody please tell me how to do this? Here is my desktop file:

[Desktop Entry]
Type=Application
Name=Trash
Comment=Trash
Icon=user-trash
Exec=nautilus trash://
Categories=Utility;
Actions=trash;

[Desktop Action trash]
Name=Empty Trash
Exec=/home/zacharygough/trash.sh -e
  • 1
    You could use Gio.FileMonitor to watch /home/$USER/.local/share/Trash. Don't forget to change the icon from idle, or else you'll have occasional crashes. What language are you using? – Jacob Vlijm Jan 9 at 8:45
  • I'm sorry, I'm a noob at this kind of stuff... Can you tell me how to use it? Also, I'm just using basic .desktop file configurations. – Zany_Zachary1 Jan 9 at 12:20
  • Ah, I see, you have this as a launcher, not programmatically. You'll need something programmatically though. Possibly you can make it change the icon in the .desktop file, if triggered. Teaching for the next few hours, I'll see if I can post a tiny script later. – Jacob Vlijm Jan 9 at 13:01
1

Automatically change the icon of a .desktop file, depending on the state of the trash can

enter image description here enter image description here

The tiny scrip uses Gio.Filemonitor to watch the state (empty or not) of trash:///

How to use

  1. Copy the script below into an empty file, name it watchout.py
  2. Replace in the lines:

    # edit path to .desktop files and icon names below
    self.fpath = "/home/jacob/Desktop/test.desktop"
    self.iconempty = "user-trash"
    self.iconfull = "user-trash-full"
    

    ...the self.fpath, and if you want the self.iconempty and the self.iconfull

  3. Run the script with the command:

    python3 /path/to/watchout.py
    

That's it!

If all works fine, add the very same command to Startup Applications.

The script

#!/usr/bin/env python3
from gi.repository import Gio, GLib

class SetTrashIcon:

    def __init__(self):
        # edit path to .desktop file and icon names below
        self.fpath = "/home/jacob/Desktop/test.desktop"
        self.iconempty = "user-trash"
        self.iconfull = "user-trash-full"
        # don't edit below
        self.trashdir = Gio.File.new_for_uri("trash:///")
        monitor = self.trashdir.monitor_directory(0, None)
        monitor.connect("changed", self.actonfile)
        self.currempty = None
        self.check_empty()
        loop = GLib.MainLoop()
        loop.run()

    def replace(self, newicon):
        # set the new icon, replace the Icon- line
        text = open(self.fpath).read()
        toreplace = [s for s in text.split() if s.startswith("Icon=")][0]
        newtext = text.replace(toreplace, "Icon=" + newicon)
        open(self.fpath, "wt").write(newtext)

    def set_icon(self, newempty):
        # if trash state changes, decide which icon to set
        if newempty != self.currempty:
            if newempty:
                self.replace(self.iconempty)
            else:
                self.replace(self.iconfull)
            self.currempty = newempty

    def check_empty(self):
        # check if trash is empty
        newempty = len(list(self.trashdir.enumerate_children(
            "standard::*", Gio.FileQueryInfoFlags.NONE, None
        ))) == 0
        self.set_icon(newempty)

    def actonfile(self, arg1=None, arg2=None, arg3=None, arg4=None):
        # act on changes in the trash content
        if arg4 == Gio.FileMonitorEvent.ATTRIBUTE_CHANGED:
            self.check_empty()

SetTrashIcon()
  • Nice. I might adapt it with a fuel gauge where you could set 4gb or whatever as full. – WinEunuuchs2Unix Jan 9 at 22:57

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.