如何始终保持桌面图标的组织,并按名称排序?
我想保持我的桌面始终按名称组织。 我怎样才能做到这一点?
桌面没有按名称组织:
按名称组织后的桌面:
按命令按字母顺序排列桌面图标
下面的脚本将重新排列桌面,如:
…按字母顺序排列的桌面如下:
排序:
- 首先是目录,然后是文件
- 从上到下,从左到右
垂直设置项目数
此外,您可以垂直设置任意数量的项目(行); 水平间距将相应自动设置。
剧本
#!/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
如何使用
-
该脚本需要
xdotool
:sudo apt-get install xdotool
-
将脚本复制到空文件中,将其另存为
arrange_dt.py
-
测试 - 通过命令运行它:
python3 /path/to/arrange_dt.py
在20秒内点击桌面,您的新安排将被应用。 如果从快捷方式运行脚本,而桌面位于前面,则会立即应用该安排。 如果桌面不在最前面,脚本将等待最多20秒。 如果时间超过,只需按F5即可应用。
-
如果一切正常,请将其添加到快捷键:选择:系统设置>“键盘”>“快捷方式”>“自定义快捷方式”。 单击“+”并添加命令:
python3 /path/to/arrange_dt.py
选项
您可以通过三种方式影响图标的排列:
-
设置“瓷砖”的大小
# set the size of the squares (indirectly, by setting n- rows) rows = 10
这将垂直设置(最大)图标数。 “瓷砖”的大小将等于(x,y)
-
设置水平偏移量
x_offs = -15
这将设置x-偏离图标矩阵的默认位置作为整体
-
设置垂直偏移
y_offs = -30
这将设置y-偏离图标矩阵的默认位置
一个例子,使用:
# set the size of the squares (indirectly, by setting n- rows) rows = 6 # set x/y offset of the matrix if you want x_offs = 50 y_offs = 10
说明
下面的解释主要是对概念的解释而不是编码
- 要按字母顺序排列图标,我们首先使用
python
的os.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
-
要查找所有其他图标的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
-
随后,我们使用以下命令将图标放在桌面上:
gvfs-set-attribute
metadata::nautilus-icon-position x,y -
最后,我们需要在前面的桌面上按F5 ,以应用更改的布局(刷新桌面)。 如果是这种情况,将立即完成。 如果没有,如果桌面在前面,脚本将在20秒内恢复,几乎按下F5并中断。 如果20秒后桌面仍然不在前面,则需要手动按F5 。