如何始终保持桌面图标井井有条,并按名称排序?

ors*_*iro 8 icons scripts unity desktop-icons

我想让我的桌面始终按名称组织。我怎样才能做到这一点?

未按名称组织的桌面:

在此处输入图片说明

按名称组织后的桌面:

在此处输入图片说明

Jac*_*ijm 9

按命令按字母顺序排列桌面图标

下面的脚本将重新排列桌面,如:

在此处输入图片说明

...进入按字母顺序排列的桌面,如:

在此处输入图片说明

订购:

  • 先目录,后文件
  • 从上到下,从左到右

垂直设置项目数

此外,您可以垂直设置任意数量的项目(行);水平间距将相应地自动设置。

剧本

#!/usr/bin/env python3
import subprocess
import os
import math
import time

# set the size of the squares (indirectly, by setting n- rows)
rows = 10
# set x/y offset of the matrix if you want
x_offs = -15
y_offs = -30

def get(cmd):
    return subprocess.check_output(cmd).decode("utf-8")

dt = get(["xdg-user-dir",  "DESKTOP"]).strip()         
# find size of the left screen
left = [int(n) for n in sum(
    [s.split("+")[0].split("x") for s in \
     get("xrandr").split() if "+0+" in s], [])]

# size of the squares (icon area)
sqr = int((left[1]/rows))

# number of cols, squares
cols = math.floor(left[0]/sqr)
n_sqrs = cols*rows

# define positions (matrix)
pos = list([[
    str(int((math.floor(n/rows)*sqr)+(sqr/2)+x_offs)),
    str(int(((n%rows)*sqr)+(sqr/2)+y_offs)),
    ] for n in range(n_sqrs)])

# list iconfiles, split into dirs and files, sort & combine
iconlist = [os.path.join(dt, item) for item in \
            sorted([item for item in os.listdir(dt) if not "~" in item])]
dirs = []; files = []
for it in iconlist:
    if os.path.isfile(it):
        files.append(it)
    else:
        dirs.append(it)
iconlist = dirs+files
# place icons in position(s)
for i, item in enumerate(iconlist):
    location = (",").join(pos[i])
    subprocess.call(["gvfs-set-attribute", "-t", "string", item,
                       'metadata::nautilus-icon-position', location])
# simulate F5 to refresh desktop, retry for max 20 secs if not in front
t = 0
while t < 40:
    w_id = [l.split()[-1] for l in get(["xprop", "-root"]).splitlines() \
        if "_NET_ACTIVE_WINDOW(WINDOW):" in l][0]
    if "desktop" in get(["xprop", "-id", w_id, "WM_CLASS"]):
        subprocess.Popen(["xdotool", "key", "F5"])
        break
    else:
        time.sleep(0.5)
        t += 1
Run Code Online (Sandbox Code Playgroud)

如何使用

  1. 脚本需要xdotool

      sudo apt-get install xdotool
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将脚本复制到一个空文件中,另存为 arrange_dt.py

  3. 通过以下命令测试运行它:

    python3 /path/to/arrange_dt.py
    
    Run Code Online (Sandbox Code Playgroud)

    在 20 秒内单击桌面,您的新安排将被应用。如果从快捷方式运行脚本,当桌面在前面时,将立即应用该安排。如果桌面不在最前面,脚本最多等待 20 秒。如果时间超过,只需按下F5即可申请。

  4. 如果一切正常,请将其添加到快捷键:选择:系统设置>“键盘”>“快捷方式”>“自定义快捷方式”。单击“+”并添加命令:

    python3 /path/to/arrange_dt.py
    
    Run Code Online (Sandbox Code Playgroud)

选项

您可以通过三种方式影响图标的排列:

  1. 设置“瓷砖”的大小

      sudo apt-get install xdotool
    
    Run Code Online (Sandbox Code Playgroud)

    这将垂直设置图标的(最大)数量。“瓷砖”的大小将等于 (x,y)

  2. 设置水平偏移

    x_offs = -15 
    
    Run Code Online (Sandbox Code Playgroud)

    这将设置与图标矩阵默认位置的 x 偏差作为一个整体

  3. 设置垂直偏移

    y_offs = -30
    
    Run Code Online (Sandbox Code Playgroud)

    这将设置与图标矩阵默认位置的 y 偏差

    一个例子,使用:

    python3 /path/to/arrange_dt.py
    
    Run Code Online (Sandbox Code Playgroud)

    在此处输入图片说明

解释

下面的解释主要是对概念的解释,而不是编码

  • 要按字母顺序定位图标,我们首先列出桌面上的项目,使用python'sos.listdir(Desktop)
  • 然后我们将文件分成两个子列表;文件/文件夹,并对两个列表进行排序,再次加入它们,首先是文件夹。
  • 然后我们创建矩阵:

    • 由于行数是在脚本的头部设置的,我们将屏幕的高度除以行数。因此,我们有图标将放置(居中)的“方块”的大小。
    • 由于图标被类似地间隔开的水平,我们就可以通过将计算的列(最大)数目宽度通过其中的图标将被放置(每图标)中的“正方形”的宽度的屏幕上,向下舍入到下面的第一个整数。
    • 在下图中,这些“虚拟”方块是可见的,红点是放置图标的地方。

      在此处输入图片说明

    • 然后我们要做的就是将第一个图标水平和垂直放置在正方形大小的一半上。

    • 要找到所有其他图标的x 位置,我们只需将它们的索引(从零开始)除以行数,向下取整。结果将添加到第一个图标的 x 位置(左上角),例如:

      item 10 (index 9): 9/4 = 2,25, rounded down: 2
      x position = position of icon 0 + 2 x the width of a square
      
      item 17 (index 16): 16/4 = 4, rounded down: 4
      x position = position of icon 0 + 4 x the width of a square
      
      Run Code Online (Sandbox Code Playgroud)
    • 要找到所有其他图标的y 位置,我们只需要索引的剩余部分和行数。结果 x 正方形的宽度将添加到第一个图标的 y 位置(左上角),例如:

      item 10 (index 9): 9%4 = 1
      y position = position of icon 0 + 1 x the height of a square
      
      item 17 (index 16): 16%4 = 0
      y position = position of icon 0 + 0 x the height of a square
      
      Run Code Online (Sandbox Code Playgroud)
  • 随后,我们将图标放置在桌面上,使用命令:

    gvfs-set-attribute <path_to_dir_or_file> metadata::nautilus-icon-position x,y
    
    Run Code Online (Sandbox Code Playgroud)
  • 最后,我们需要按F5 桌面在前,应用更改后的布局(刷新桌面)。如果是这种情况,它将立即完成。否则,如果桌面在前面,脚本会在 20 秒内重试,实际上是按下F5并中断。如果 20 秒后桌面仍然不在前面,则需要手动按F5