Ubuntu 18.04 上 Dash to Dock 中的动态垃圾桶图标

Ubuntu 18.04 上 Dash to Dock 中的动态垃圾桶图标

我想为我的 Dock 制作一个垃圾图标。Dash 到 Dock没有可以添加的垃圾图标,所以我打算做一个。我想要垃圾箱已满当有东西被添加到垃圾箱时,图标,我想要清空垃圾垃圾桶里没有东西时显示的图标。有人能告诉我怎么做吗?这是我的桌面文件:

[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

根据垃圾桶的状态自动更改 .desktop 文件的图标

在此处输入图片描述 在此处输入图片描述

Gio.Filemonitor用于监视状态(空或非空)的小脚本trash:///

如何使用

  1. 将以下脚本复制到一个空文件中,并命名watchout.py
  2. 替换以下行:

    # 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"
    

    ...self.fpath如果你想要self.iconemptyself.iconfull

  3. 使用以下命令运行脚本:

    python3 /path/to/watchout.py
    

就是这样!

如果一切正常,请将相同的命令添加到启动应用程序。

剧本

#!/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()

相关内容