How can I open thunar so that it selects specific file?

Building upon answer from theY4Kman, this is how to do it without 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:""

The only caveat is that you need to separate folder path and file name.


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


With thunar's built-in command line switches, you can't. If you see man thunar, you'll figure that you could only open a folder that way, but you won't be able to preselect a file in it.

Does it mean that you can't do it at all?

Fortunately not, but you'll need help from external programs. An example that accomplishes this using xdotool to send ctrl+s and type in filename (which effectively is going to select it):

#!/bin/sh
file=$1
[ -z "$file" ]; then
    echo 'No file selected' 1>&2
    exit 1
fi

if [[ ! $(command -v thunar) ]]; then
    echo 'Thunar is not installed' 1>&2
    exit 1
fi

if [ -d "$file" ]; then
    thunar "$file" &
else
    if [ ! -f "$file" ]; then
        echo 'File does not exist' 1>&2
        exit 1
    fi

    if [[ ! $(command -v xdotool) ]]; then
        echo 'Xdotool is not installed' 1>&2
        exit 1
    fi

    set -e #exit on any error
    thunar "$(dirname "$file")" &
    window_id=`xdotool search --sync --onlyvisible --class 'Thunar' | head -n1`
    xdotool key --clearmodifiers 'ctrl+s'
    xdotool type "$(basename "$file")"
    xdotool key Return
fi

Usage: script /path/to/file-or-folder

There are two caveats:

  1. You'll notice slight lag due to xdotool --sync, but I believe it's acceptable.
  2. This won't work with files hidden in thunar for any reason, such as dotfiles.

Tags:

Thunar