MACSCRPT Archives

July 2015

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, 31 Jul 2015 23:56:35 +1000
Content-Type:
text/plain
Parts/Attachments:
text/plain (58 lines)
Excuse me if this is a double post...


On 31 Jul 2015, at 3:22 pm, SUBSCRIBE MACSCRPT Inyo55 <[log in to unmask]> wrote:
> 
> Osx = Yosemite.  App to see code from recording is Acrobat Pro 9. 

You're out of luck with Acrobat -- it's not recordable.

> Scripting the Acrobat is where I wanted to record the steps to bring into the applescript.  Say, extract pages 1 thru 12, save new file.  Then go back to original pdf, extract pages 13 thru 42, whatever, save as new file.  Repeat as required

Why not skip Acrobat altogether?


use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"
use framework "Quartz" -- for PDF stuff

on extractPages:firstPage thruTo:lastPage ofPDFDocAt:posixPath
	--  make URL of the first PDF
	set inNSURL to current application's class "NSURL"'s fileURLWithPath:posixPath
	-- make PDF document from the URL
	set theDoc to current application's PDFDocument's alloc()'s initWithURL:inNSURL
	-- count the pages
	set pageCount to theDoc's pageCount()
	-- delete pages at end
	if lastPage < pageCount then
		repeat with i from pageCount to (lastPage + 1) by -1
			(theDoc's removePageAtIndex:(i - 1)) -- zero-based indexes
		end repeat
	end if
	-- delete pages at start
	if firstPage > 1 then
		repeat with i from (firstPage - 1) to 1 by -1
			(theDoc's removePageAtIndex:(i - 1)) -- zero-based indexes
		end repeat
	end if
	-- save in new file
	set newPath to inNSURL's |path|()'s stringByDeletingPathExtension()'s stringByAppendingString:("_" & firstPage & "-" & lastPage & ".pdf")
	theDoc's writeToFile:newPath
end extractPages:thruTo:ofPDFDocAt:

set thePath to POSIX path of (choose file of type {"pdf"})
my extractPages:2 thruTo:5 ofPDFDocAt:thePath



You could also do the original text extraction and grep in AppleScript.

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

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

ATOM RSS1 RSS2