A Weird Imagination

Thunar custom actions

The problem#

While I use the command-line and keyboard-based interfaces a lot to manage files, I also regularly use graphical and mouse-based interfaces. As with any time there's different ways of interacting with a system, there's some times when in one mode when you want the features of the other.

The solution#

Xfce's file manager Thunar has a feature called custom actions, which lets you add menu items that run commands on files or directories you are viewing or selecting.

The documentation suggests multiple ideas of actions you might want, including converting among different formants, rotating images, and various file management actions like viewing disk usage and bulk moving files. Additionally, the default action "Open Terminal Here" is one I use frequently.

The details#

Defining custom actions#

Thunar custom actions are defined as a command to run and a filter of which files it should appear for.

The command is run in a shell, so it can do things like include for loops. Since quickly gets unwieldy to put in a single-line text box, if the script is more than just calling a command, you probably want to put the script into a file and have to action reference that file.

One thing to keep in mind is that there's no terminal, so anything the action outputs to the terminal will never get seen. You can use zenity or notify-send for easy ways to display messages graphically. zenity can also be used to get input from the user.

The filtering can be on either filename patterns or a list of file types (audio, image, etc.). Notably, this means the filtering cannot depend on the contents of the file, so if you have an action that only makes sense on some files, then it will need to handle possibly getting files that don't match somehow.

Example custom actions#

As mentioned above, the documentation suggests many custom actions, and I make good use of the default "Open Terminal Here" action.

Additionally, I've defined "🧹 Remove empty directories" as a custom action that's just the command rmdir * filtered to directories. Since rmdir only removes empty directories, that can safely be run anywhere, and it's a file management task I do somewhat frequently.

Play motion photos#

Based on my previous blog post, I wrote a script play-motion-photos.sh and assigned to a custom action "📹 Play Motion Photos" with the command /path/to/play-motion-photos.sh %F filtered to Image Files with the pattern *.jpg;*.JPG;*.jpeg;*.JPEG:

#!/bin/bash

seen_any=
for file in "$@"
do
  if [[ -n "$(exiftool "$file" -EmbeddedVideoFile)" ]]
  then
    seen_any=true
    exiftool "$file" -b -EmbeddedVideoFile | vlc -
  fi
done
if [[ -z "$seen_any" ]]
then
  zenity --info --text \
    "None of the given files have a motion photo."
fi

(Replace vlc - with mplayer - -cache 8092 if you prefer mplayer to vlc.)

As mentioned above, since the file name/type is insufficient to determine if there's a motion photo to play, this script uses zenity to inform the user when it doesn't find any motion photos. Otherwise, it wouldn't do anything at all in that case, making the user wonder if the script actually ran.

Send To#

Thunar's Send To menu offers a similar extension point, allowing scripts to be added to a menu, possibly filtered on the type of file selected. Depending on the semantics of the action, some actions may fit better as a "send to" action than a generic custom action.

Other terminal-graphical interactions#

As I started talking about the getting between the command-line and graphical file management interfaces more generally, here's a few ways to work across both:

Comments

Have something to add? Post a comment by sending an email to comments@aweirdimagination.net. You may use Markdown for formatting.

There are no comments yet.