MACSCRPT Archives

April 2014

MACSCRPT@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Shane Stanley <[log in to unmask]>
Reply To:
Macintosh Scripting Systems <[log in to unmask]>
Date:
Fri, 4 Apr 2014 19:31:45 +1100
Content-Type:
text/plain
Parts/Attachments:
text/plain (39 lines)
On 4 Apr 2014, at 1:55 pm, Nobumi Iyanaga <[log in to unmask]> wrote:

> I have many pdf files for which I would like to change the pagination -- for example reverse it completely, from the last page up to the first page, or invert (put page 1 as page 2, and page 2 to as page 1; page 4 as page 3, and page 3 as page 4, and so on).
> 
> I would like to ask if there is any tool that can automate such tasks, and how it is possible.

If you're running Mavericks, you can do it in AppleScriptObjC. Save the following as an ASObjC-based library (that is, as a .scptd file in ~/Library/Script Libraries/, with Cocoa-AppleScript checked in the Bundle Contents drawer):

use framework "Foundation"
use framework "Quartz"

on reversePDF:thePath outputTo:newPath
	set inNSURL to current application's NSURL's fileURLWithPath:thePath
	set outNSURL to current application's NSURL's fileURLWithPath:newPath
	set theDoc to current application's PDFDocument's alloc()'s initWithURL:inNSURL
	set theCount to theDoc's pageCount() as integer
	set firstPage to 0
	set lastPage to (theCount - 1)
	repeat with i from 1 to (theCount div 2)
		(theDoc's exchangePageAtIndex:firstPage withPageAtIndex:lastPage)
		set firstPage to firstPage + 1
		set lastPage to lastPage - 1
	end repeat
	theDoc's writeToURL:outNSURL
end reversePDF:outputTo:

Then you'd call it from your script like this:

use theLib : script "<name of script lib>"

set thePath to "/Users/shane/Desktop/Whatever.pdf"
set newPath to "/Users/shane/Desktop/Whatever-2.pdf"
theLib's reversePDF:thePath outputTo:newPath


-- 
Shane Stanley <[log in to unmask]>
<www.macosxautomation.com/applescript/apps/>

ATOM RSS1 RSS2