【发布时间】:2018-06-27 19:54:41
【问题描述】:
我想构建一个 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 可安装工具来做我想做的事吗?
【问题讨论】:
-
@Erotemic,我可能愿意探索 SomeGuyOnAComputer 的建议。您将需要访问以下目录:
~/.local/share/nautilus-python/extensions。您是否对该目录具有写入权限,并且将文件放在那里对您来说是一个可行的解决方案? -
这不是很清楚你的最终目标。从我的角度来看,我想您想知道 nautilus 或任何应用程序是否打开了此目录?在这种情况下,您需要使用一些系统功能。
-
我的最终目标是把它放在我的 gvim-toolkit (github.com/Erotemic/vimtk) 中,它提供了与窗口系统交互的命令。这个问题与 nautilus 后端有关。所以,我只关心当前是否有一个 nautilus 窗口在后台显示目录。 (如果它是打开的,我的应用程序只会使窗口成为焦点,否则它将打开它)。
标签: python ubuntu nautilus wmctrl