就像标题一样。在 Windows 上,我可以这样做:
explorer /select,"C:\folder\file.txt"
Run Code Online (Sandbox Code Playgroud)
这将导致打开explorer.exe
,即会立即打开C:\folder
并选择file.txt
。
我相信 ROX 也有这个功能。
我可以对 thunar 做同样的事情吗?
小智 9
基于来自 的回答theY4Kman
,这是没有 Python 的方法:
dbus-send --type=method_call --dest=org.xfce.Thunar /org/xfce/FileManager org.xfce.FileManager.DisplayFolderAndSelect string:"/home/user/Downloads" string:"File.txt" string:"" string:""
Run Code Online (Sandbox Code Playgroud)
唯一需要注意的是,您需要将文件夹路径和文件名分开。
通过一点点挖掘,我发现使用 D-Bus 是可能的:
#!/usr/bin/env python
import dbus
import os
import sys
import urlparse
import urllib
bus = dbus.SessionBus()
obj = bus.get_object('org.xfce.Thunar', '/org/xfce/FileManager')
iface = dbus.Interface(obj, 'org.xfce.FileManager')
_thunar_display_folder = iface.get_dbus_method('DisplayFolder')
_thunar_display_folder_and_select = iface.get_dbus_method('DisplayFolderAndSelect')
def display_folder(uri, display='', startup_id=''):
_thunar_display_folder(uri, display, startup_id)
def display_folder_and_select(uri, filename, display='', startup_id=''):
_thunar_display_folder_and_select(uri, filename, display, startup_id)
def path_to_url(path):
return urlparse.urljoin('file:', urllib.pathname2url(path))
def url_to_path(url):
return urlparse.urlparse(url).path
def main(args):
path = args[1] # May be a path (from cmdline) or a file:// URL (from OS)
path = url_to_path(path)
path = os.path.realpath(path)
url = path_to_url(path)
if os.path.isfile(path):
dirname = os.path.dirname(url)
filename = os.path.basename(url)
display_folder_and_select(dirname, filename)
else:
display_folder(url)
if __name__ == '__main__':
main(sys.argv)
Run Code Online (Sandbox Code Playgroud)
执行:
$ ./thunar-open-file.py /home/user/myfile.txt
Run Code Online (Sandbox Code Playgroud)
如果你通过它,它仍然会打开一个文件夹:
$ ./thunar-open-file.py /home/user/
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2077 次 |
最近记录: |