Skip to content

Instantly share code, notes, and snippets.

@paucoma
Last active February 23, 2021 17:06
Show Gist options
  • Save paucoma/5f3268275205197d218fb48d296c71e5 to your computer and use it in GitHub Desktop.
Save paucoma/5f3268275205197d218fb48d296c71e5 to your computer and use it in GitHub Desktop.
Nemo Filemanager script to convert image to pdf with img2pdf with single or multiple file arguments
#!/bin/bash
# Description: This Nemo script uses img2pdf with single or multiple file arguments.
# script location: ~/.local/share/nemo/scripts/
# Resources:
# [img2pdf Homepage](https://gitlab.mister-muffin.de/josch/img2pdf)
# [img2pdf mirror on github](https://github.com/josch/img2pdf)
# Tags: img2pdf nemo-filemanager nemo-script
#arguments passed to img2pdf
myoptions="--pagesize A4 --fit shrink"
#creating temporary file for error/warning log if applies
logpath="$(mktemp /tmp/ns_toPDF.XXXXXX)"
argsIn=()
for f in "$@"; do
#Lets test that inputs are all of image type
fmime=$(file -b --mime-type "$f")
if [ "${fmime%/*}" != "image" ]; then
echo "Warning: $f is of type $fmime, only image type, ignoring" >> $logpath
else
argsIn+=("${f// /\ }")
fi
done
if [ ${#argsIn[@]} -gt 0 ]; then
# IFS set to <space><tab><newline> by default. `$ echo -n "$IFS" | hexdump -cC`
# to be able to pass filenames with spaces through sort we temporarily change it.
prevIFS="$IFS"
IFS=$'\n'
# we sort them, passed through printf with newline separators, sort default separator
srtdargs=($(printf "%s\n" "${argsIn[@]// /\ }" |sort))
IFS="$prevIFS"
# the path and extension from the first ordered arg
outfile="${srtdargs[0]##*/}"
outfile="${outfile%.*}"
# if spaces we substitute the "bad habit" for underscores
outfile="${outfile// /_}.pdf"
# check if file exists to warn about overwriting
if [ -f $outfile ]; then
zenity --question --title="Warning : File already exists" --icon-name='dialog-warning' --no-wrap \
--text="$outfile Already Exists \n Are you sure you want to replace ?"
fi
if [ $? -eq 0 ]; then
#here is the actual execution of the command
img2pdf $myoptions -o $outfile "${srtdargs[@]// /\ }" &>> $logpath
fi
else
echo "No files to process...they were probably filtered out" >> $logpath
fi
# if $logpath file exists and size is greater than 0, this script has something to say.
if [ -s $logpath ]; then
zenity --text-info --width=600 --height=400 --title="Console Log" --filename=$logpath
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment