0
votes

So i have a PDF file and i'm using Automator to generate JPG files from each slide. Then i want to loop through each of these images and create side-by-side photos from them. So page 1 and page 2 will create an image, then page 3 and 4 too, and so on... I have an Applescript action so far where the input is all of the image files:

on run {input, parameters}

    set selectedFiles to {}

    repeat with i in input
        copy (POSIX path of i) to end of selectedFiles
    end repeat

    return selectedFiles
end run

The function what i want to do is this, which is an ImageMagick function two merge to images next to eachother:

do shell script "convert " +append 01.jpg 02.jpg 01&2.jpg

How can i add this function inside my Applescript above?

Or maybe it would be easier to do with Shell script?

1

1 Answers

0
votes

I'm in a bit of rush so not my best piece of code, but you can do it in bash like this:

#!/bin/bash
# pdfsheets
#
# Passs the name of a PDF as parameter and get it as a bunch of double-page spreads called "sheet-0.jpg" ... "sheet-n.jpg"
#
# Pick up parameter
pdf=$1
echo "Processing document: $pdf"

# Split document into individual pages each as JPEG
convert "$pdf" page-$$-%03d.jpg

# Get names of pages into array and see how many we got
pages=( $(ls page-$$-*.jpg) )
npages=${#pages[@]}
echo DEBUG:npages:$npages

# Check if odd number of pages - synthesize empty white one at end if odd
if [ $((npages%2)) -ne 0 ]; then
   lastpage=${pages[@]:(-1)}
   echo DEBUG:lastpage:$lastpage
   newlast=$(printf "page-$$-%03d.jpg" $npages)
   convert "$lastpage" -threshold -1 $newlast
   pages=( $(ls page-$$-*.jpg) )
   npages=${#pages[@]}
fi
s=0
for ((i=0;i<npages;i+=2)) do
   a=${pages[i]}
   b=${pages[((i+1))]}
   out=$(printf "sheet-%d.jpg" $s)
   echo Converting $a and $b to $out
   convert $a $b +append $out
   ((s++))
done

Run it like this and get output as follows:

./pdfsheets document.pdf
Processing document: document.pdf
DEBUG:npages:5
DEBUG:lastpage:page-49169-004.jpg
Converting page-49169-000.jpg and page-49169-001.jpg to sheet-0.jpg
Converting page-49169-002.jpg and page-49169-003.jpg to sheet-1.jpg
Converting page-49169-004.jpg and page-49169-005.jpg to sheet-2.jpg