我想构建一个 Python 脚本来检查特定目录是否在 nautilus 中打开。
到目前为止,我最好的解决方案是使用wmctrl -lxp
列出所有窗口,这给了我这样的输出:
0x0323d584 0 1006 nautilus.Nautilus namek Downloads
0x0325083a 0 1006 nautilus.Nautilus namek test
0x04400003 0 25536 gvim.Gvim namek yolo_voc.py + (~/code/netharn/netharn/examples) - GVIM4
然后我检查我感兴趣的目录的基本名称是否在窗口的窗口名称中nautilus.Nautilus
。
这是我刚刚描述的不完整解决方案的代码:
def is_directory_open(dpath):
import ubelt as ub # pip install me! https://github.com/Erotemic/ubelt
import platform
from os.path import basename
import re
computer_name = platform.node()
dname = basename(dpath)
for line in ub.cmd('wmctrl -lxp')['out'].splitlines():
parts = re.split(' *', line)
if len(parts) > 3 and parts[3] == 'nautilus.Nautilus':
if parts[4] == computer_name:
# FIXME: Might be a False positive!
line_dname = ' '.join(parts[5:])
if line_dname == dname:
return True
# Always correctly returns False
return False
这绝对可以确定它是否未打开,这只能让我到目前为止,因为它可能会返回误报。如果我想检查是否/foo/test
打开,我无法判断第二行是指该目录还是其他路径,最终目录名为test
. 例如,我无法/foo/test
区分/bar/test
.
有什么方法可以在 Ubuntu 上使用内置或 apt-get / pip 可安装工具来做我想做的事吗?