How to open thunar so that it selects specific file

thunar

Like in title. On Windows, I can do this:

explorer /select,"C:\folder\file.txt"

which will result in opening of explorer.exe, that will immediately open C:\folder and select file.txt.

I believe ROX had this functionality too.

Can I do the same with thunar?

Best Answer

With a little digging, I discovered this is possible using 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)

Execute with:

$ ./thunar-open-file.py /home/user/myfile.txt

And it will still open a folder, if you pass that:

$ ./thunar-open-file.py /home/user/

screencast for hardcore proof

Related Question