Thunar
如何打開 thunar 以選擇特定文件?
就像標題一樣。在 Windows 上,我可以這樣做:
explorer /select,"C:\folder\file.txt"
這將導致打開
explorer.exe
,即會立即打開C:\folder
並選擇file.txt
。我相信 ROX 也有這個功能。
我可以對 thunar 做同樣的事情嗎?
經過一番探勘,我發現使用 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)
執行:
$ ./thunar-open-file.py /home/user/myfile.txt
如果你通過它,它仍然會打開一個文件夾:
$ ./thunar-open-file.py /home/user/
基於來自 的回答
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:""
唯一需要注意的是,您需要將文件夾路徑和文件名分開。