Q: Applescript to Duplicate PDF (x) number of times
Is is possible to duplicate a PDF a variable number of times?
Ideally I want to take a single page PDF and duplicate the page {n} number of times within the PDF file, to create a multiple page PDF.
I'd be launching this from Esko Automation Engine and passing the PDF and the variable quantity to the script - applescript or shell script.
Any help would be greatly appreciated!
Posted on Aug 8, 2016 12:21 AM
Phillip,
Don't confuse this script with Hiroto's RubyCocoa conversion to PyObjC of his original code — with what I am posting here. Different application goals.
The following Python code has been "hardened somewhat for input errors, and is designed to return error codes per Eskoo. It takes one or more single page PDF files, and based on those specified documents, an output directory location, and an integer duplication factor — all specified on the command-line, it will update each PDF in-place with the inserted duplications of the original document page. It will work either as the Bash script that I have posted below, or as a standalone Python program.
Assumption: The script is being run in the same folder as the input files, or a relative or full path is specified for each input file.
#!/bin/bash
#
# $1 : input files separated by ':'
# $2 : output directory
# $3 : additional parameters - duplication count
# exit : 0 = OK, 1 = Warning, 2 = Error
#
# Criterion: Process one or more PDF files that contain only one page.
# Based on supplied integer value, insert n-tuple additional
# copies of this first page into the PDF, and update same.
#
# Usage: pdf_duplicate.sh file1.pdf:file2.pdf:filen.pdf /path/to/outdir dup_cnt
# Standalone: pdf_duplicate.py <same arguments as above>
# Required: OS X System Python with Scripting Bridge support
#
# V1.2, tested with Python 2.7.10 on OS X 10.11.6
# VikingOSX, Aug 2016, Apple Support Communites
python <<EOF - "$@"
# coding: utf-8
from Foundation import NSURL
from Quartz.PDFKit import PDFDocument
import os
import sys
rtn = 0
def usage():
print("Usage: {} file.pdf path/to/outdir dupe_cnt".format(sys.argv[0]))
print("Usage: {} file1.pdf:file2.pdf:filen.pdf path/to/outdir dupe_cnt".
format(sys.argv[0]))
print("Usage: {} ~/path/file1.pdf:~/path/filen.pdf path/to/outdir dup_cnt".
format(sys.argv[0]))
sys.exit(2)
def main():
if len(sys.argv) < 4:
usage()
sys.exit(2)
if sys.argv[1].count(':'):
pdf_files = [os.path.expanduser(x) for x in sys.argv[1].split(':')]
else:
pdf_files = os.path.expanduser(sys.argv[1])
if os.path.isdir(os.path.expanduser(sys.argv[2])):
out_dir = os.path.expanduser(sys.argv[2]).rstrip('/')
else:
print("{} Output directory: {} - does not exist".
format(sys.argv[0], sys.argv[2]))
sys.exit(2)
if sys.argv[3].isdigit():
dup_page_cnt = int(sys.argv[3])
else:
print("{} Duplication count must be an integer".format(sys.argv[0]))
sys.exit(2)
for name in pdf_files:
url = NSURL.fileURLWithPath_(name)
pdf = PDFDocument.alloc().initWithURL_(url)
if not pdf:
print("Skipping: {} - Not a valid PDF file".format(name))
rtn = 2
continue
if pdf.pageCount() == 1:
for newpage in range(0, dup_page_cnt):
pdf.insertPage_atIndex_(pdf.pageAtIndex_(0), newpage)
else:
print("Skipping: {} - Already more than one page".format(name))
rtn = 2
continue
outname = os.path.join(out_dir, os.path.basename(name))
if not pdf.writeToFile_(outname):
print("Could not save PDF: {}".format(name))
rtn = 2
if __name__ == '__main__':
main()
sys.exit(rtn)
EOF
exit
Posted on Aug 9, 2016 2:44 AM