Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript Open PDF save as JPG

I can do a batch process in Photoshop pretty easily, but I need it be a little more robust.

I want to grab a folder full of PDFs then open each PDF in Photoshop. Then check to the width and height of the document. If the file is wider than it is tall it runs one Photoshop action. If it is taller than it is wide, it runs another action.

Thoughts on how to accomplish this?

-Kirk

Quad 2.5Ghz G5, Mac OS X (10.4.8)

Posted on Oct 10, 2006 8:13 AM

Reply
12 replies

Oct 10, 2006 8:29 AM in response to W. Kirk Lutz

I was thinking I could open the PDF in Acrobat Professional first and somehow save the width and height. Then open them in Photoshop and run the Photoshop action.

I was able to do this in Quark back in the OS9 days using this(I substituted Acrobat for Quark)

tell application "Adobe Acrobat 7.0 Professional"
activate
set pageWidth to «class widt» of bounds of «class CUPG» as «class FXPT» as real --Gets the width of the current page
set pageHeight to «class hegt» of bounds of «class CUPG» as «class FXPT» as real --Gets the height of the current page
end tell

Oct 11, 2006 1:38 AM in response to W. Kirk Lutz

Kirk, here is something of mine that you can have a look at and modify to suit your needs. I have put the bones of what you wanted in it. Photoshop works out the height/width part after reasterizing the file. I don't know whats in you actions but I personally try not to use them and code the whole thing this way your script will be more portable as it no longer requires actions. Hope this helps.
--
set tempFolderName to "Converted to CMYK JPEG at 300dpi"
set inputFolder to choose folder
tell application "Finder"
set filesList to (files of inputFolder whose file type is "PDF ")
if (not (exists folder ((inputFolder as string) & tempFolderName))) then
set outputFolder to make new folder at inputFolder with properties {name:tempFolderName}
else
set outputFolder to folder ((inputFolder as string) & tempFolderName)
end if
end tell
repeat with aFile in filesList
set fileIndex to 0
tell application "Finder"
set theFile to aFile as alias
set theFileName to name of theFile
end tell
tell application "Adobe Photoshop CS"
set display dialogs to never
set UserPrefs to properties of settings -- Record user settings
set ruler units of settings to cm units -- to be used for the canvas resize, remove if NOT required…
set background color to {class:RGB color, red:255, green:255, blue:255}
open theFile as PDF with options {class:PDF open options, mode:CMYK, resolution:300, use antialias:true, constrain proportions:true}
set docRef to the current document
tell the current document
set docHeight to height of docRef -- to be used for the canvas resize, remove if NOT required…
set docWidth to width of docRef -- to be used for the canvas resize, remove if NOT required…
flatten
-- this line below cuts OFF the 1cm to all edges around my print PDF's
-- removing bleed and registration marks, remove if NOT required…
resize canvas width (docWidth - 2) height (docHeight - 2)
if (docHeight > docWidth) then
do action "Your Action 1" from "Your Set" -- Note this is case sensitive.
else
do action "Your Action 2" from "Your Set" -- Note this is case sensitive.
end if
set docName to name of docRef
set docBaseName to getBaseName(docName) of me
set newFileName to (outputFolder as string) & docBaseName
end tell
set ruler units of settings to ruler units of UserPrefs -- Reset changed user settings
save docRef in file newFileName as JPEG with options ¬
{class:JPEG save options, embed color profile:true, format options:optimized, matte:background color matte, quality:9} appending lowercase extension with copying
close current document without saving
end tell
end repeat
on getBaseName(fName)
set baseName to fName
repeat with idx from 1 to (length of fName)
if (item idx of fName = ".") then
set baseName to (items 1 thru (idx - 1) of fName) as string
exit repeat
end if
end repeat
return baseName
end getBaseName
--

Oct 11, 2006 7:54 AM in response to W. Kirk Lutz

Kirk, in my case the files do have .jpg at the end of the name. The script uses a handler to strip the file name down to the first "." there may be an issue with this what do your PDF file names look like? may give me a clue. As for the multi-page PDF not to my knowlegde but I use CS1 it could be a feature of CS2. In my workflow eveything is output as single page PDF's so I've never needed to use such a feature.

Oct 11, 2006 8:14 AM in response to Mark Larsen

My PDFs look normal filename.pdf the results I get are just filename, no filename.jpg

I've been able to manually open all pages in a PDF through Photoshop by holding down the Option key and selecting each individual page.

I can go into Adobe Acrobat and chop the PDF up into individual pages.

Any experience scripting Macromedia Fireworks? Or ImageReady?

The final file will be 72dpi and optimized for web delivery.

-kirk

Oct 11, 2006 8:31 AM in response to W. Kirk Lutz

Kirk, I have not scripted either of these yet. I use scripts to help some of our web guy's by processing print graphics to a format they can use. I usually give them a bunch of processed files which they run batch actions on to do the final part. SFW only made Photoshop scripting in CS2 there is a bug in this feature (there is a workaround for it) there is also a bug with the resize command (which there is also a workaround for) my main reasons for not upgrading just yet. What is the function of your 2 actions? is this the resizing part or does it do other stuff?

Oct 12, 2006 2:03 AM in response to W. Kirk Lutz

Kirk, have a very brief look at fit image. I must say I've never used this feature before and Im a long time user of PS. I've coded what I think it does you will have to test and see for yourself. I have also moved a few things about as they did not need to be part of the repeat loop in PS. Also included convert to RGB color space as files are to be for web. Can you also check PS's pref's file handling set append file extension to always and lower case if it is not already.
--
property fitImage : 1280 / 854 -- Your fit image scale.
--
set tempFolderName to "Converted to RGB JPEG"
set inputFolder to choose folder
tell application "Finder"
set filesList to (files of inputFolder whose file type is "PDF " or name extension is "pdf")
if (not (exists folder ((inputFolder as string) & tempFolderName))) then
set outputFolder to make new folder at inputFolder with properties {name:tempFolderName}
else
set outputFolder to folder ((inputFolder as string) & tempFolderName)
end if
end tell
tell application "Adobe Photoshop CS" -- Moved out of repeat loop only need to do this once.
set UserPrefs to properties of settings -- Record user settings
set ruler units of settings to pixel units -- Easier to use for web stuff
set background color to {class:RGB color, red:255, green:255, blue:255}
end tell
repeat with aFile in filesList
set fileIndex to 0
tell application "Finder"
set theFile to aFile as alias
set theFileName to name of theFile
end tell
tell application "Adobe Photoshop CS"
activate
set display dialogs to never
open theFile as PDF with options {class:PDF open options, mode:CMYK, resolution:300, use antialias:true, constrain proportions:true}
set docRef to the current document
tell docRef
if (mode of docRef is not RGB) then
change mode docRef to RGB
end if
set docHeight to height of docRef
set docWidth to width of docRef
flatten
if (docHeight > docWidth) then -- This is what I think fit image does.
set imagePro to (docHeight / docWidth)
if imagePro < fitImage then
resize image width 854 resolution 72
else
resize image height 1280 resolution 72
end if
end if
if (docWidth > docHeight) then
set imagePro to (docWidth / docHeight)
if imagePro < fitImage then
resize image height 854 resolution 72
else
resize image width 1280 resolution 72
end if
end if
set docName to name of docRef
set docBaseName to getBaseName(docName) of me
set newFileName to (outputFolder as string) & docBaseName
end tell
save docRef in file newFileName as JPEG with options ¬
{class:JPEG save options, embed color profile:true, format options:optimized, matte:background color matte, quality:9} ¬
appending lowercase extension with copying
close current document without saving
end tell
end repeat
tell application "Adobe Photoshop CS" -- Reset changed user settings after repeat loop.
set ruler units of settings to ruler units of UserPrefs
end tell
--
on getBaseName(fName)
set baseName to fName
repeat with idx from 1 to (length of fName)
if (item idx of fName = ".") then
set baseName to (items 1 thru (idx - 1) of fName) as string
exit repeat
end if
end repeat
return baseName
end getBaseName
--

Oct 17, 2006 11:01 AM in response to Mark Larsen

The new script almost works. It doesn't do the image size thing correctly though. Fit image looks at the final proportional dimensions. So if an image is wider than it is tall, it won't automatically make it 1280 wide. Sometimes even though an image is wider than it is tall, when you make it 1280 wide the height is larger than 854.

So it is very close. But not quite there. I really appreciate your help.

-Kirk

Oct 19, 2006 5:13 AM in response to W. Kirk Lutz

Kirk, I misunderstood what you were doing with Fit image by using 2 actions I thought you may be transposing the figures. What I now think you want was much simpler to do. Remove the first property line altogether.

Then replace lines between "flatten" and "set docName to name of docRef"
with the following
--
if (docHeight > docWidth) or (docHeight = docWidth) then
resize image height 854 resolution 72
else
resize image width 1280 resolution 72
end if
--

Applescript Open PDF save as JPG

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.