Print selected files with the right-click context menu in Caja

There is something new:
In the Ubuntu-it forum, a user helped me gather all those commands above in one script.
The script can be in two variants:
for the default printer
for a printer specified in the script

This script can act in different ways:

  • Selecting one or more files of various types at the same time (PDF, text, images, LibreOffice formats).
  • Right- click in the empty space in an open folder, without selecting any files. All the printable files inside the folder will be printed.
  • Selecting a closed folder . As in the previous case, all the printable files inside the folder will be printed.

The script is able to recognize the type of file selected. If you accidentally select a non-printable file (for example an .mp3 ), then an error message will be returned. Otherwise the appropriate commands for printing will be executed.

Copy the script "Print" (for default printer) in the .config/caja/scripts folder and make it executable.

#!/bin/bash

# Print: (according to the selection)
#       all selected files
#       all files in the only selected folder
#       all files in current folder (no selection)
IFS=$'\n' # don't use <space>,<tab> to split words as part of expansion
# TODO eliminate the need to change $IFS
PRINTER=$(lpstat -d | grep -Eo "\S+$")
folder="$PWD"
printable="PDF|PNG|JPEG|SVG|OpenDocument|UTF-8|ASCII|Microsoft|Windows"
# FIXME
# .doc & .xls created by Excel/Word are matched only if edited by LibreOffice
# in case of no selection: print all files in current folder
if [ $# -eq 0 ]; then
    selected=( $(ls "$folder") )
# if only a folder is selected, it's our target: print all its files
elif [[ $# == 1 && -d "$1" ]]; then
    folder="${CAJA_SCRIPT_SELECTED_FILE_PATHS%$'\n'}" # strip trailing newline
    selected=( $(ls "$folder") )
# else: print all selected files
else
    selected=($@)
fi
for f in ${selected[@]}; do
    fileType=$(file -b "$folder/$f" | grep -Eo "$printable")
    case $fileType in
        OpenDocument|Microsoft|Windows )
            libreoffice -pt $PRINTER "$folder/$f"
        ;;
        PDF|PNG|JPEG|UTF-8|ASCII )
            lpr -P $PRINTER "$folder/$f"
        ;;
        SVG )
            # depends on imagemagick
            convert "$folder/$f" png:- | lpr -P $PRINTER
        ;;
        * )
            notify-send -i error "Not printed '$f'"
        ;;
    esac
    [ $? -eq 0 ] || notify-send -i error "ERROR printing '$f'"
done

If you want to specify a different printer than the default one, find the name of the printer you are using by typing in the terminal :

lpstat -p -d

and replace Samsung-M2020-Series with the name of your printer.

#!/bin/bash
# Print: (according to the selection)
#       all selected files
#       all files in the only selected folder
#       all files in current folder (no selection)
IFS=$'\n' # don't use <space>,<tab> to split words as part of expansion
# TODO eliminate the need to change $IFS
PRINTER=Samsung-M2020-Series$
folder="$PWD"
printable="PDF|PNG|JPEG|SVG|OpenDocument|UTF-8|ASCII|Microsoft|Windows"
# FIXME
# .doc & .xls created by Excel/Word are matched only if edited by LibreOffice
# in case of no selection: print all files in current folder
if [ $# -eq 0 ]; then
    selected=( $(ls "$folder") )
# if only a folder is selected, it's our target: print all its files
elif [[ $# == 1 && -d "$1" ]]; then
    folder="${CAJA_SCRIPT_SELECTED_FILE_PATHS%$'\n'}" # strip trailing newline
    selected=( $(ls "$folder") )
# else: print all selected files
else
    selected=($@)
fi
for f in ${selected[@]}; do
    fileType=$(file -b "$folder/$f" | grep -Eo "$printable")
    case $fileType in
        OpenDocument|Microsoft|Windows )
            libreoffice -pt Samsung-M2020-Series "$folder/$f"
        ;;
        PDF|PNG|JPEG|UTF-8|ASCII )
            lpr -P Samsung-M2020-Series "$folder/$f"
        ;;
        SVG )
            # depends on imagemagick
            convert "$folder/$f" png:- | lpr -P Samsung-M2020-Series
        ;;
        * )
            notify-send -i error "Not printed '$f'"
        ;;
    esac
    [ $? -eq 0 ] || notify-send -i error "ERROR printing '$f'"
done

Wiki

NB When you copy the script to a new pluma file, delete the spaces before #!/bin/bash in the first line

2 Likes