PyObjC to ASOC ex2.

This example will add empty page at end of PDF file.

The code was once again converted from Python to ASOC.

use framework "Foundation"
use framework "Quartz"
use scripting additions

(**
* #!/usr/bin/python
* # coding=utf-8
*
* # ADDPAGE v.1.4 : Adds a blank page to the END of any PDF file(s) sent as arguments.
*
* # by Ben Byram-Wigfield.
*
* # Page size of blank page is taken from first page of PDF.
* # Can be used as Automator action or as shell script.
*
*
* from Quartz import PDFDocument, PDFPage, kPDFDisplayBoxMediaBox
* import sys
* from Foundation import NSURL
*
*
* mediabox = kPDFDisplayBoxMediaBox
*
*
* def addPage(filename):
*	filename = filename.decode('utf-8')
*	pdfURL = NSURL.fileURLWithPath_(filename)
*	pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL)
*	if pdfDoc:
*		pageNum = pdfDoc.pageCount()
*		page = pdfDoc.pageAtIndex_(0)
*		pageSize = page.boundsForBox_(mediabox)
*		blankPage = PDFPage.alloc().init()
*		blankPage.setBounds_forBox_(pageSize, mediabox)
*		pdfDoc.insertPage_atIndex_(blankPage, pageNum)
*		pdfDoc.writeToFile_(filename)
*	return
*
* if __name__ == '__main__':
*	for filename in sys.argv[1:]:
*		addPage(filename)
*)

property mediabox : a reference to current application's kPDFDisplayBoxMediaBox

set theFile to POSIX path of (choose file with prompt "Choose a PDF file:")
set writeFilePath to POSIX path of (path to desktop) & "new_addPage.pdf"

my addPageURLWithPath:theFile saveTo:writeFilePath

on addPageURLWithPath:theURL saveTo:writeURL
	set pdfURL to current application's |NSURL|'s fileURLWithPath:theURL
	set pdfDoc to current application's PDFDocument's alloc()'s initWithURL:pdfURL
	
	set writeFile to current application's |NSURL|'s fileURLWithPath:writeURL
	
	if (pdfDoc is not missing value) then
		set pageNum to pdfDoc's pageCount() as integer
		set page to pdfDoc's pageAtIndex:0
		set pageSize to page's boundsForBox:mediabox
		set blankPage to current application's PDFPage's alloc()'s init()
		blankPage's setBounds:pageSize forBox:mediabox
		pdfDoc's insertPage:blankPage atIndex:pageNum
		pdfDoc's writeToURL:writeFile
	end if
end addPageURLWithPath:saveTo: