• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Can Acrobat PDF's be saved as single pages via Applescript
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can Acrobat PDF's be saved as single pages via Applescript


  • Subject: Re: Can Acrobat PDF's be saved as single pages via Applescript
  • From: Brian Christmas <email@hidden>
  • Date: Sun, 08 Jan 2017 15:11:44 +1100

Thank you for clearing that up for me Shane. And especially thank you for all the code. Must have taken you ages! My extensive testing found that only saving as TIFFS or JPEGS direct from Acrobat trimmed to the crop box. Took a long time to find that out!

A question, though, if you don’t mind. It’s essential that the image size be preserved 100%, and while I assume that will be the case, I must ask if it is so? Going to a bitmap is new territory for me.

I don’t think I’ll have time to insert and test your code for several hours. I’m in the habit of fixing/updating ‘retired’ or broken iMacs, and donating them to friends and acquaintances who have never had a Mac in their life before, sometimes not even any sort of PC. I’m setting up one for my wife's widowed sister-in-law, and we’re delivering it tomorrow, so it’s taking priority right now. Fixed it up (a busted 2011 27” i7 iMac) with a 250GB SSD, 2 TB internal, and a 3TB Time Machine drive. First computer she’ll ever own. It FLIES. Games for the grandkids on it too.

Regards, and thanks everyone for all the advice.

Santa



On 8 Jan 2017, at 2:30 pm, Shane Stanley <email@hidden> wrote:

On 8 Jan 2017, at 9:20 am, Brian Christmas <email@hidden> wrote:

Any advice, please?

You've misunderstood Yvan's advice, and haven't followed Takaaki's code. You don't try to wrap it in an Acrobat tell block -- you use it instead of Acrobat. Unfortunately that's not the end of the story, because it won't honor the crop box and produce a cropped tif.

But you can use a variation on it to produce a cropped bitmap and then save that as a pdf file, without have to save any intermediary tif file. Again, this is instead of Acrobat:

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "Quartz" -- for PDFs
use framework "AppKit"

-- build file URLs
set originalPath to POSIX path of (choose file of type {"com.adobe.pdf"})
set originalURL to (current application's |NSURL|'s fileURLWithPath:originalPath)
set originalNameStub to originalURL's lastPathComponent()'s stringByDeletingPathExtension()
set destFolderURL to originalURL's URLByDeletingLastPathComponent()
-- read PDF
set aPDFdoc to current application's PDFDocument's alloc()'s initWithURL:originalURL
set pCount to aPDFdoc's pageCount()
-- work through pages
repeat with i from 1 to pCount
set thisPage to (aPDFdoc's pageAtIndex:(i - 1))
set theBounds to (thisPage's boundsForBox:(current application's kPDFDisplayBoxCropBox))
set theSize to |size| of theBounds
-- make image from page
set thisImage to (current application's NSImage's alloc()'s initWithData:(thisPage's dataRepresentation()))
if thisImage = missing value then error "Error in getting imagerep from PDF in page:" & (i as string)
-- crop by drawing into new image of crop size
set newImage to (current application's NSImage's alloc()'s initWithSize:theSize)
-- draw from original to new
newImage's lockFocus()
(thisImage's drawAtPoint:{0, 0} fromRect:theBounds operation:(current application's NSCompositeSourceOver) fraction:1.0)
newImage's unlockFocus()
-- create image view and add cropped image to it
set theView to (current application's NSImageView's alloc()'s initWithFrame:{{0, 0}, theSize})
(theView's setImage:newImage)
-- get PDF data from image view and save it
set theData to (theView's dataWithPDFInsideRect:{{0, 0}, theSize})
set outFileName to current application's NSString's stringWithFormat_("Page %@ %@.pdf", i, originalNameStub)
set outURL to (destFolderURL's URLByAppendingPathComponent:outFileName)
(theData's writeToURL:outURL atomically:true)
end repeat

-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>

Do not post admin requests to the list. They will be ignored. AppleScript-Users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: Archives: http://lists.apple.com/archives/applescript-users This email sent to email@hidden
  • Follow-Ups:
    • Re: Can Acrobat PDF's be saved as single pages via Applescript
      • From: Shane Stanley <email@hidden>
References: 
 >Can Acrobat PDF's be saved as single pages via Applescript (From: Brian Christmas <email@hidden>)
 >Re: Can Acrobat PDF's be saved as single pages via Applescript (From: Yvan KOENIG <email@hidden>)
 >Re: Can Acrobat PDF's be saved as single pages via Applescript (From: Brian Christmas <email@hidden>)
 >Re: Can Acrobat PDF's be saved as single pages via Applescript (From: Shane Stanley <email@hidden>)

  • Prev by Date: Re: Can Acrobat PDF's be saved as single pages via Applescript
  • Next by Date: Re: Can Acrobat PDF's be saved as single pages via Applescript
  • Previous by thread: Re: Can Acrobat PDF's be saved as single pages via Applescript
  • Next by thread: Re: Can Acrobat PDF's be saved as single pages via Applescript
  • Index(es):
    • Date
    • Thread