如何打开 thunar 以便它选择特定文件?

rr-*_*rr- 6 thunar

就像标题一样。在 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)

唯一需要注意的是,您需要将文件夹路径和文件名分开。

  • 谢谢!请注意,`org.xfce.FileManager.DisplayFolderAndSelect` 是 XFCE 特定的,但今天有一种更便携的方法可用:```dbus-send --type=method_call --dest=org.freedesktop.FileManager1 /org/freedesktop /FileManager1 org.freedesktop.FileManager1.ShowItems array:string:"file:///path/to/file_to_focus" string:""``` - 它适用于 XFCE4(在 Xubuntu 20.04 上测试)和 GNOME(在 Ubuntu 20.04 上测试) ) 和 KDE(在 Kubuntu 20.04 上测试)和 Budgie(在 Ubuntu Budgie 20.04 上测试)和 Mate(在 Ubuntu Mate 20.04 上测试)。- 值得注意的是,它*不适用于 LXQt(在 Lubuntu 20.04 上测试) (2认同)

the*_*man 6

通过一点点挖掘,我发现使用 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)

硬核证明的截屏视频