Files (nautilus)

Date: 2023/05/20 (initial publish), 2023/05/23 (last update)

Source: en/note-00043.md

Previous Post Top Next Post

TOC

This is based on nautilus version 43.2 in 2023 (Debian/Bookworm).

Gnome Files

“Gnome Files” (a.k.a nautilus) is the default file manager for GNOME desktop environment. It is rather a simple file manager with primitive functionalities, but it can be extended to offer rich features.

gnome-terminal started from nautilus using extension

The nautilus-extension-gnome-terminal package is automatically installed when gnome-terminal is installed and enables nautilus GUI to start gnome-terminal CLI session. There are 2 ways.

The current directory of started terminal is intuitively set by GUI.

You can copy file/directory paths selected on the GUI dialogue to the terminal prompt.

This is the most useful extension of nautilus for me and is written in C++ using libnautilus-extension4 as its backend.

Practical extension

If you want to run a simple script on GUI selected files, practical approach is to use the simple script-type extension.

Here is an example to convert iPhone HEIC image into JPEG and Microsoft Word document into PDF. You can see its execution log with journalctl -f

#!/bin/sh
__exec() {
  systemd-cat -p 3 -t "${0##*/}" "$@"
}

if [ "$NAUTILUS_SCRIPT_CURRENT_URI" = "${NAUTILUS_SCRIPT_CURRENT_URI#file://}" ]; then
  # not starting with file://
  __exec echo "Not local path: '$NAUTILUS_SCRIPT_CURRENT_URI'"
else
  echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | {
    while true; do
      read FILE_PATH
      case "$FILE_PATH" in
      *.HEIC | *.heic)
        __exec heif-convert "$FILE_PATH" "${FILE_PATH%.*}.jpeg"
        ;;
      *.doc | *.wri | *.rtf | *.odt | *.DOC | *.WRI | *.RTF)
        __exec lowriter --headless --convert-to pdf "$FILE_PATH"
        ;;
      "")
        break
        ;;
      *)
        __exec echo "Unknown file: '$FILE_PATH'"
        ;;
      esac
    done
  }
fi

This is very trivial coding and very handy extension of nautilus.

My latest scripts are at: https://github.com/osamuaoki/nautilus-scripts

Extension methods explained

There are mainly 3 paths to extend nautilus.

Extension using libnautilus-extension4 from C/C++ code

This is a full-featured extension. This requires serious learning before using.

Extension using python3-nautilus from Python code

This is a full-featured extension. This requires less learning before using but it’s non-trivial.

You must install python3-nautilus for this.

You can process selected files etc., by placing an extension Python code which subclasses Nautilus.MenuProvide to python3-nautilus in ~/.local/share/nautilus-python/extensions.

Due to recent changes of nautilus and GTK(3->4), some published extensions don’t function as expected. So this is non-trivial.

Extension using script interface of nautilus

This is a simpler and older interface to nautilus.

You can run a script on selected files etc., by placing a script in ~/.local/share/nautilus/scripts. nautilus passes its selection to script using the value of certain environment variables.

Notable variables:

Review of extensions

There are many extensions. I am mostly using basic Debian packaged extensions only. Here are my thoughts on them. I may be incorrect.

organize.py may be adopted to use python3-nautilus with some GUI to select output directory style, or even adjust timestamps.

Reference sites

Previous Post Top Next Post