Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

"PDF Open Options" in Applescript

New Here ,
Sep 25, 2009 Sep 25, 2009

So I am trying to batch convert a bunch of PDFs to JPEG files as part of a larger Applescript, and I'm finding that some of the parameters in "PDF Open Options" are ignored. Namely, the "height", "width" and "constrain proportions" parameters.

This code is lifted directly from the Photoshop CS3 Scripting Guide (with filename changed, of course):

tell application "Adobe Photoshop CS3"

set myFilePath to alias "WABEL0457937:Users:Charles:Desktop:8925.pdf"

with timeout of 10000 seconds

open myFilePath as PDF with options {class:PDF open options, height:pixels 100, width:pixels 200, mode:RGB, resolution:72, use antialias:true, page:1, constrain proportions:false}

end timeout

end tell

In the resulting file, the "resolution" is correct, but the height and width are calculated using the PDF's original height and width multiplied by the resolution, and the image is constrained to the original proportions.

I thought it might be a collision with specifying the resolution and the height/width in pixels, so I tried omitting the resolution, but then it just defaults to 300.

Anyone else create a script that opens PDFs and run into this?

TOPICS
Actions and scripting
4.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
LEGEND ,
Sep 25, 2009 Sep 25, 2009

I don't know anything specific about AppleScript or converting PDFs, but it makes sense that original dimensions of the PDF would be used.

So what happens if you omit the height/width in pixels and supply a different resolution?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 25, 2009 Sep 25, 2009

It would make sense for Photoshop to use the original dimensions of the PDF… but not when "constrain proportion" is false. And also not when a specific width in pixels has been set.

If I specify a resolution of 150, it creates a PDF with 150 DPI resolution, and makes the pixel size larger. The resolution is being respected.

My goal is to be able to open PDFs of varying sizes and proportions, but have them all 1000 pixels at their longest dimension.

Presently, I have to "overshoot" and make an extremely large file (to make sure that small PDFs are rendered large enough) and then shrink it down before saving. This works on the small PDFs (with a width of 2" for example), but for the really large PDFs (over 13") this means a very long wait to rasterize, sometimes over five minutes, and it slows the process down to a crawl.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 26, 2009 Sep 26, 2009

If you set only the height to 1000 and DPI and Constrain=true does it work for vertical PDFs?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 27, 2009 Sep 27, 2009

Well I only have CS2 and according to the dictionary terms for the app 'height', 'width' & 'constrain proportions' are all 'DEPRECATED' as of CS2 and NO longer used. So you may want to look and see if you can do something like this to get the dimensions of the PDF file first then you could calculate what resolution you want to then open it at.

set This_PDF to POSIX path of (choose file without invisibles)

try

set PDF_Height to do shell script "/usr/bin/mdls -name kMDItemPageHeight" & space & quoted form of This_PDF

end try

try

set PDF_Width to do shell script "/usr/bin/mdls -name kMDItemPageWidth" & space & quoted form of This_PDF

end try

This will be in points…

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Sep 27, 2009 Sep 27, 2009

For what it is worth they are also deprecated in the CS3 Javascript guide.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 28, 2009 Sep 28, 2009

PDF open options are like this…

{class:PDF open options, bits per channel:eight, crop page:crop box, mode:CMYK, page:1, resolution:72, suppress warnings:true, use antialias:true, use page number:false}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 28, 2009 Sep 28, 2009
LATEST

Thanks for that!

(If those parameters are deprecated since CS2, it's probably bad form for Adobe to use them in their documentation for CS3. Just sayin'!)

Anyway, it looks like the shell script you quoted gives the page height in points with an assumed resolution of 72 DPI. I can use that with my target dimension to calculate what to set the resolution to.

In case it helps anyone else, here's the working code I came up with:

-- ("str_replace" is a custom function to find and replace text in a string, it's not built-in to Applescript)

set pageHeight to do shell script "/usr/bin/mdls -name kMDItemPageHeight " & quoted form of (POSIX path of this_item as string)

set pageHeight to my str_replace("kMDItemPageHeight = ", "", pageHeight)

set pageWidth to do shell script "/usr/bin/mdls -name kMDItemPageWidth " & quoted form of (POSIX path of this_item as string)

set pageWidth to my str_replace("kMDItemPageWidth = ", "", pageWidth)

if (pageHeight as number) is greater than (pageWidth as number) then

set pdf_resolution to round (1000 / (pageHeight as number) * 72) rounding up

else

set pdf_resolution to round (1000 / (pageWidth as number) * 72) rounding up

end if

try

with timeout of 5 seconds

open alias ("WABEL0457937:Users:Charles:Desktop:Test:image_prep:" & image_id & ".pdf") as PDF with options {class:PDF open options, resolution:pdf_resolution, mode:RGB, use antialias:true, suppress warnings:false, use page number:true, page:1, crop page:media box}

end timeout

on error

-- I have some sections here that check for and handle progress windows, password prompts, and such

-- so the script can either continue or fail gracefully

end try
All of the images coming out of this script are just over 1000 pixels at their widest dimension. Since it's not critical that they be exactly 1000 (just very close), I was able to completely eliminate the resizing portion of the script to save even more processing time.
Thanks for the pointers, everyone!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines