Extend Xfce’s Thunar File Manager with Custom Actions

9322

Thunar is the Xfce desktop environment’s file manager. It’s incredibly lightweight, flexible — but largely unappreciated outside Xfce circles. Thunar has a selection of extensions, and allows users to create custom actions that can extend its functionality even further. Let’s take a look at how to customize Thunar to get the most out of your Linux desktop.

Thunar’s custom actions are used to take action on files and folders found within Thunar and can do anything from bulk-renaming to running specialized scripts. For anyone that uses Thunar, custom actions is a must-know feature. Let’s get started!

How Custom Actions Work

Thunar custom actions work by creating special conditional commands that work on specific file types within a folder. These custom, conditional commands use special parameters to know how they are going to be run within Thunar. The parameters are crucial to the actions and are:

  • %f — The path to the first selected file.
  • %F — The paths to all selected files.
  • %d — Directory containing the file passed in %f.
  • %D — Directories containing the files passed in %F.
  • %n — The first selected file name (no path.)
  • %N — the selected file names (without paths.)

Obviously, when dealing with multiple files or directories, the parameter to be used will be a capital letter and if the directories must be passed along with the file names, the parameter with the corresponding case must be used. In other words if %F is used then %D must be the parameter used if the directory pathes must also be passed to the command.

Creating a Custom Action

I will demonstrate the creation of custom actions first, by adding an action to Thunar that will open a text file (such as a configuration file) with administration privileges. This particular action is incredibly handy for those who like to edit configuration files (such as smb.conf or apache2.conf) in a graphical editor such as Gedit. Creating this custom action is simple. The first step is to open up the custom actions window. To do this open Thunar and click Edit > Configure Custom Actions. With the Custom Actions window open, click the “+” button to create a new custom action (see Figure 1.)

Thunar

With the Create Action window open the following information must be entered, in the Basic tab, for this particular action:

  • Name: Edit with sudo
  • Description: Edit a text file with administrative privileges.
  • Command: gksu gedit %f
  • Icon: Click the Icon button and then select an icon to represent the action.

The Appearance and Condition tab needs attention as well. Click on that tab and configure the following:

  • File pattern: *
  • Appears if selection contains: Text Files

When everything has been configured, click OK to save the Custom Action. Before this action can be used, Thunar must be restarted, so close the Thunar window and reopen the application. When Thunar is reopened navigate to the /etc directory and locate a configuration file (such as /etc/samba/smb.conf) and right-click the file. When the context menu opens up a new entry will appear named “Edit with sudo” (or whatever was entered for the “Name” option above). Select the Edit with sudo entry and the sudo password prompt will appear. Upon proper sudo authentication, the configuration file will open in the Gedit application with administrative permission.

Other Handy Custom Actions

I want to offer up a few more actions, to illustrate how flexible this system is. The first action works to convert MP3 files to the more open source friendly OGG format. This action includes a necessary script. The contents of the script (name it mp3toogg.sh) are:

#!/bin/bash

for TRACK in "$@" ; do

# allow filenames containing dots such as "an artist feat. some_other_artist"
OGGOUT=$(ls "$TRACK" |sed 's/(.*)..*/1/')

gst-launch filesrc location="$1" ! decodebin ! audioconvert ! vorbisenc name=enc quality=0.7 ! oggmux ! filesink location="$OGGOUT.ogg" done

Create and save that file in the /usr/local/bin directory, making sure users have permission to use the file as well as execute the file (The command sudo chmod u+x /usr/local/bin/mp3toogg.sh will give the file executable permission.) The above file will require the following tools:

  • gst-launch
  • vorbisenc

If either of the above applications are not installed, install them using the Add/Remove Software tool. Once everything is installed and in place, it’s time to create the Thunar Action that will call the script. Run through the same process as with the “Edit with sudo” action, only using the below information:

  • Basic Tab
  • Name: mp3 to ogg
  • Description: Convert mp3 files to ogg
  • Command: mp3toogg.sh %F
  • Appearance and Conditions Tab
  • File Pattern: *
  • Appears if selection contains: Audio Files

Save this action, restart Thunar, open a folder containing mp3 files, right-click on an mp3 file and behold the new action. If that action is clicked, the selected mp3 file(s) will be converted into the ogg format.

Another really handy action to have is the ability to right-click a text-file or image and send it directly to a printer. This action will not work with .doc or .odt type documents, but with text and image files it works like a champ. To create this action, the necessary information is:

  • Basic Tab
  • Name: Send to printer
  • Description: Print text and images
  • Command: lp %f
  • Appearance and Conditions Tab
  • File pattern: *
  • Appears if selection contains: Text Files and Image Files

Here’s a really great action that uses the K3B CD/DVD Burning suite to burn ISO images. The details for this action are:

  • Basic Tab
  • Name: Burn ISO
  • Description: Burn an ISO image with K3B
  • Command: k3b --cdimage %f
  • Appearance and Conditions tab
  • File pattern: *.iso
  • Appears if selection contains: Other files

Roll your own

The ability to create Customized Actions with Thunar makes this particular tool incredibly flexible. In fact, the limitations on these actions is only limited to the imagination and skill of the user. Simply enough, that equates to being able to roll actions that stretch the boundaries of the standard file manager. I invite Linux.com readers to share their customized actions here in the comments.