Tuesday, November 8, 2016

Merge PDFs with MacOS and the command line.

I use the command line to merge PDFs all of the time, but I have found that with every MacOS release, things move. I made a script, PDFconcatenator.sh, that finds the script join.py in /System/Library and then passes arguments to join.py. join.py seems to move with every MacOS release and this script works as long as you have join.py on the system.

Ad example of three files:
PDFconcatenator.sh -o merged.pdf ../../file1..pdf file3pdf /off/root/3.pdf
#!/bin/bash
#Before anything else, set the PATH_SCRIPT variable
    pushd `dirname $0` > /dev/null; PATH_SCRIPT=`pwd -P`; popd > /dev/null
    PROGNAME=${0##*/}; PROGVERSION=0.1.0
# WHAT DOES THIS DO?
# This script looks at MacOS distribution and tries to find the python script to merge
# pdfs files. It tends to move depending on MacOS releases.

HUNT_DIR="/System/Library/"

usage(){
    echo "$PROGNAME"
    echo "This program finds the PDF concatenation script on MacOS "
    echo "example:"
    echo "$PROGNAME -o PATH/TO/YOUR/MERGED/FILE.pdf /PATH/TO/1.pdf /PATH/TO/2.pdf /3.pdf"
    echo ""
}

# lowercase makes things lowercase. This is an issue because sometime different OS
# have different conventions. bsd and BSD or FreeBSD and freebsd
lowercase(){
    echo "$1" | sed "y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/"
}

# This is my canned script to determine the OS
#
getOS(){
OS=`lowercase \`uname\``
KERNEL=`uname -r`
MACH=`uname -m`

if [ "${OS}" = "windowsnt" ]; then
    OS=windows
elif [ "${OS}" = "darwin" ]; then
    OS=mac
else
    OS=`uname`
    if [ "${OS}" = "SunOS" ] ; then
OS=Solaris
ARCH=`uname -p`
OSSTR="${OS} ${REV}(${ARCH} `uname -v`)"
    elif [ "${OS}" = "AIX" ] ; then
OSSTR="${OS} `oslevel` (`oslevel -r`)"
    elif [ "${OS}" = "Linux" ] ; then
if [ -f /etc/redhat-release ] ; then
DistroBasedOn='RedHat'
DIST=`cat /etc/redhat-release |sed s/\ release.*//`
PSUEDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//`
REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//`
elif [ -f /etc/SuSE-release ] ; then
DistroBasedOn='SuSe'
PSUEDONAME=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//`
REV=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ //`
elif [ -f /etc/mandrake-release ] ; then
DistroBasedOn='Mandrake'
PSUEDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//`
REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//`
elif [ -f /etc/debian_version ] ; then
DistroBasedOn='Debian'
DIST=`cat /etc/lsb-release | grep '^DISTRIB_ID' | awk -F= '{ print $2 }'`
PSUEDONAME=`cat /etc/lsb-release | grep '^DISTRIB_CODENAME' | awk -F= '{ print $2 }'`
REV=`cat /etc/lsb-release | grep '^DISTRIB_RELEASE' | awk -F= '{ print $2 }'`
fi
if [ -f /etc/UnitedLinux-release ] ; then
DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]"
fi
OS=`lowercase $OS`
DistroBasedOn=`lowercase $DistroBasedOn`
readonly OS
readonly DIST
readonly DistroBasedOn
readonly PSUEDONAME
readonly REV
readonly KERNEL
readonly MACH
fi
fi
}

# start by getting the OS to be sure we can run
getOS

if [ "mac" != "$OS" ]; then
    echo "This script only works on MacOS"
    exit 1
fi

SCRIPT=$(find $HUNT_DIR -name join.py 2>/dev/null)
SCRIPT=$( echo "$SCRIPT" | sed 's/ /\\ /g')

if [ "$#" -eq 0 ]; then
    usage
    exit 2
fi

#run the concatenation
echo "Executing the following command:"
echo "$SCRIPT $@"
#execute the actual command
eval "$SCRIPT $@"


No comments:

Post a Comment