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


#1

With these 2 scripts in Caja context menu, you can print files directly, there’s no need to open them :
Check that Cups is installed.

1) Print ALL the PDF files contained in a folder.

With this script, all pdf files are printed, even if only one is selected. It works with the default printer.

for FILE in *.pdf *.PDF ; do lpr “$FILE” ; done

Copy to a text editor, save as “Print ALL PDFs in the folder” and give permission to allow executing file as program.
Put it in the script folder of Mate (CTRL H to show hidden files):
/home/user/.config/caja/scripts,
so you can have it in the context menu - Script.

2) Print selected PDF files (it works for me with PDF, JPEG and PNG, TXT but not with the libreoffice formats):

#!/bin/bash
printer=printer_name
echo “$CAJA_SCRIPT_SELECTED_FILE_PATHS” | while read file
do
lpr -P “$printer” “$file”
done
exit 0

Do the same procedure to allow execution and put it in the script folder.
Replace the printer name, which can be obtained by the command on terminal

lpstat -p -d

or see the /etc/cups/printers.conf file

example: printer=Samsung-M2020-Series


#2

Here is how to print .odt etc.

/usr/lib/libreoffice/program/swriter -p /home/andy/Documents/Beacon_Check_Register.odt


#3

OK!
I turned it into a script for the context menu, it prints the Libreoffice selected files as they appear in print preview and prints also Jpg, Png, txt and PDF files.
I tried it on the ods and odt files.
Change Samsung-M2020-Series with your printer.

3) Print selected files

#!/bin/bash
printer=Samsung-M2020-Series
echo “$CAJA_SCRIPT_SELECTED_FILE_PATHS” | while read file
do
/usr/lib/libreoffice/program/swriter -p “$printer” “$file”
done
exit 0


#4

Great job. :slight_smile: I will try your script.


#5

The above script is based on Libreoffice.
For those using OpenOffice (I have them both), it works for me with this:

#!/bin/bash
printer=Samsung-M2020-Series
echo “$CAJA_SCRIPT_SELECTED_FILE_PATHS” | while read file
do
openoffice4 -writer -p -invisible “$printer” “$file”
done
exit 0

Furthermore, giving libreoffice --help or openoffice4 -h on a teminal, you have all the command options.
An interesting example: --pt that is used to print with a specific printer.


#6

Default printer

If you do not want to specify the name of the printer, in order to always print on the default default printer (if multiple printers are connected it will be enough to change the default printer), use these:

Print selected PDF files

 #!/bin/bash
 echo "$CAJA_SCRIPT_SELECTED_FILE_PATHS" | while read file
       do
           lpr -P  "$file"
    done
    exit 0

Print selected LibreOffice files:

#!/bin/bash
echo "$CAJA_SCRIPT_SELECTED_FILE_PATHS" |  while read file
    do
/usr/lib/libreoffice/program/swriter -p --invisible "$file"
done
exit 0 0

Delete print queue:

Through this additional script you can delete the print queue. You can insert it in the scripts folder or create a launcher on the desktop or on the panel:

#!/bin/bash
cancel -a
exit 0

#7

How-to with Caja-Actions
With Caja-Actions you can create context menus for Caja and you can associate with scripts or programs.
You can find it here or in synaptic.

Specific printer with the name of the printer after –pt (to find the printer name, type lpstat -p -d on the terminal, then replace Samsung - M2020 - Series)

Print Libreoffice files

Path: libreoffice
Parameters: --pt printer-name %F


Print PDF, TXT, JPG,PNG

Path: lpr
Parameters: -P printer-name %F


All configurations in this Imgur album:


#8

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=([email protected])
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=([email protected])
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


How to test Caja-Actions on Ubuntu MATE 14.04 and 14.10
Tips, Tutorials and Guides Index
#9

Other useful commands for a launcher :

to cancel pendig print jobs :
cancel -a

cancel all print jobs and delete the print job history
cancel -a -x

cancel only the latest one print job
lprm


#10

The scripts “Print” or “Print with …” on Caja- Actions work on the selected files, in this way you have the command directly in the context menu, without going through the Script sub-menu.

That is to say you don’t have the two additional features (whether a closed folder is selected, or by right-clicking on the empty space of an open folder), as it happens putting the script with the other method (.config/caja/scripts)
The error message for non-printable files occurs regularly.

Path: bash
Parameters: /home/username/config/caja/scripts/Print% B
that is the path of the script with the addition of the% B parameter


#11

@Mary Could you give the place where you found the printer-c̶o̶d̶e̶c̶s̶ ̶ drivers? As we have a brother printer for the office and I cannot access it on wifi.


#12

@ThanksAMillion, click on the search icon in the upper right corner and type in “brother printers”. You’ll find a wealth of information. Good luck.


#13

To print double-sided PDF on Ubuntu Mate, put these two scripts in the hidden folder /home/user/.config/caja/scripts and call them:

Print odd pages

#!/bin/bash

echo "$CAJA_SCRIPT_SELECTED_FILE_PATHS" | while read file
    do
       lpr -o page-set=odd "$file"
done
exit 0

and
Print even pages

#!/bin/bash

echo "$CAJA_SCRIPT_SELECTED_FILE_PATHS" | while read file
   do
       lpr -o page-set=even -o outputorder=reverse "$file"
done
exit 0

First print the odd pages.
Turn the sheets with the printed side face down.
And then print the even pages.