Thunar as music player

~/img/thumbnails.png (904x587 416kB)
Screenshot depicting the filemanager Thunar with
many classic music albums showing

I recently configured my systems thumbnailer to create thumbnails for directories that contains a folder.jpg. This has been a standard feature of Microsoft windows explorer since at least windows XP. And it is especially nice for music collections.

Now my music directory looks nice, but if i want to play an album (directory) in mpv from Thunar, i couldn’t do so with a simple keybinding. From the filemanagers perspective the albums are just directories. And there are many times when i also want to open a album directory to browse the songs/files within.

Essentially what i wanted was to open a item with a command, different from it’s default. To open something normally one would press Return (or double-click), so the obvious keybinding to open with alternative command would be: Shift+Return

I like these types of rice, where the same trigger (shift-return keybinding), will do different things depending on the target. So instead of having multiple custom actions and keybindings to maintain and remember you can now have just this one and it will work on any file.


Below are some images explaining how I set up the custom action in Thunar.

~/img/uca-create.png (996x951 200kB)
Screenshot of GUI file manager Thunar open with custom actions settings
  1. Select Configure custom actions... from the edit menu.
  2. Click on the + to add a new Custom action.
  3. Enter details for custom action, note %F after command.
  4. Add a keyboard shortcut (Shift+Return)
~/img/uca-setup.png (558x518 239kB)
Screenshot of custom action windows Appearance Condition settings
  1. In the same ‘Create Action’ window, open the tab: Appearance Conditions
    and check all the boxes.

This setup will work on any file or directory, and it will work with multiple items selected (%F).

Now you only need a script with the same name as command (shift-return), available in your $PATH.

Below is an example that will open sub-directories to my MUSIC directory (obtained with xdg-user-dir MUSIC) in mpv, but only if they contain audio files.

If the file is an image, it is opened with gimp.

./shift-return
#!/bin/bash

for path in "$@"; do
  mime_type=$(file --brief --mime-type "$path")
  if [[ -d $path && $path =~ ^$(xdg-user-dir MUSIC)/ ]]; then
    for format in mp3 flac m4a; do
      audio_files=("$path"/*."$format")
      [[ -f ${audio_files[0]} ]] || continue
      open_in_mpv+=("${audio_files[@]}")
    done
  elif [[ $mime_type =~ ^image ]]; then
    open_in_gimp+=("$path")
  fi
done

[[ ${open_in_gimp[*]} ]] && exec gimp "${open_in_gimp[@]}"
[[ ${open_in_mpv[*]}  ]] && exec mpv  "${open_in_mpv[@]}"

Since we want to support opening of multiple files, we first for loop over each path and store files to be opened in one of the arrays, open_in_mpv or open_in_gimp. And only if any of the array contain valid files we open the program.

Otherwise it gets wonky if f.i. gimp isn’t running, the first file would open gimp, and block the script till gimp was closed.

There is however a problem with the script related to mpv. If mpv is running when the script is executed, this script will open a second instance of mpv..

I have a solution for that, but that’s for a future post! ;-)