PDF to Cropped JPEGs using Applescript and Photoshop CS5

Hi All,

I have a Cover PDF (Back + Front) and I want an applescript to achieve the following:

  1. Using the Applet, drag & drop the Cover PDF file to open it in Photoshop CS5 with default settings

  2. Change the units to Inches

  3. Dialog box to get the input from the user for the new width (nwidth)

  4. Change the Canvas Size width to nwidth anchor position middle left

  5. Save-As the cropped image as JPG (quality:12) with suffix “_Back Cover” in the same location as the Cover PDF

  6. Revert the Cover PDF to its original state using History:Open

  7. Change the units to Inches

  8. Change the Canvas Size width to nwidth anchor position middle right

  9. Save-As the cropped image as JPG (quality:12) with suffix “_Front Cover” in the same location as the Cover PDF

  10. Close the Cover PDF without saving

Can anyone please help me write a script.

Thanks in advance.
Masood

Hello Masterminds, No reply so far. Is it so hard to interpret this into an applescript?

Hi masoodahmad,

I was thinking that you might want to add something that can do this into your library.

gl,
kel

Me, and I think that I speak for others too, are here to help people who have problems with their script or have questions about scripting. This is a do-you-want-to-built-a-script-for-me question, something that most people don’t respond to. Unless you say it’s job and are willing to pay for it obviously.

My suggestion is that you download the AppleScript Language Guide and start scripting. When you get stuck you get help pretty quickly around here.

BTW: Before asking, here is a starting point for your question without the use of Photoshop but image events.

Hi All,

The script has nothing to do with any kind of job. I create JPGs from PDF for my personal record.
If you are not willing to help me, then please give me some starting point i.e. how to open a pdf (no fixed source location) in Photoshop CS5 using Applescript droplet. and then use the filename of that PDF as a variable for further scripting.

I mean to say, drag the pdf on the droplet to open it in photoshop cs5.

Moreover, if anyone can find a url for some good reading material like: Applescript with Photoshop CS5, then that will be a great help.

I’m not a script writer. I’m a Layout Artist by profession and mainly work in InDesign, Photoshop, etc.

Thanks in advance.

Hi,

I think I have an answer to your first question:

set f to choose file
tell application "TextEdit"
	open f
end tell

Good Luck,
kel

knowing something doesn’t gave you the rights to insult someone. I already said that I do not know anything about scripting. Therefore, you should be precise in your response. If you do not want to help, then just say “NO” rather than giving me hints to troubleshoot.

Why are you giving me examples to open the file in text editor, when I asked that I want a droplet that will open a file in Photoshop CS5. Let me explain as I explain it to my child:
I have a pdf file somewhere and I want to open that pdf file in photoshop by dragging it onto the droplet.

After opening, there are certain tasks that needs to be performed via script (I already explained in my first post).

If anyone have the script, then share it else RELAX.

I’ll close this thread than.

Hi,

as a starting point this droplet opens PDF files in Photoshop.
You have to save the code as application.

I’m not that familiar with scripting Photoshop to figure out the other steps


on open theItems
	repeat with anItem in theItems
		tell application "System Events" to set fileExtension to name extension of anItem
		if fileExtension is "pdf" then
			tell application "Adobe Photoshop CS6"
				open file (anItem as text)
				
				-- do stuff
				
			end tell
		end if
	end repeat
end open

Hi masoodahmad,

I want give you some input…

First of all, if your PDF is a generated for print, it contains Trim Box and Bleed Box areas so you need to open it in Photoshop specifying to crop it based on Trim Box.

In the below sample the PDF is opened specifying a height and resolution (this is an example for generate web preview)

tell application "Adobe Photoshop CS5"
	set myOptions to {class:PDF open options, height:600 as pixels, mode:RGB, resolution:72, use antialias:true, page:1, constrain proportions:true, crop page:trim box}
	open file pathCopertina as PDF with options myOptions showing dialogs never
end tell

About resize canvas this can be solved with the following command:

resize canvas current document width 300 as pixels height 600 as pixels anchor position bottom left

Se documentation is PS Dictionary

But if you don’t have any scripting experience is like a climb Everest for a beginner…

Stefano - AME

Thanks Stefan and ame,

I also came up with something (though I’m not a script writer), Everything was working fine, except that I’m only getting one jpg at the end.

Secondly, I want to suffix “_Back cover” and “_Front Cover” to my saved jpegs file. How can I achieve this automatically, without typing any name in the save dialog box.

If you look at the script, you will notice that two jpegs are being saved, however, I’m getting only one as the latter overrides the previous one.

@ame, thanks, my pdf doesn’t contain any crop marks, but this will be helpful.


tell application "Finder"
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {"."}
	
	set theLocation to (target of front window) as string
	
	set x to text returned of (display dialog "Enter the page width (in inches)" default answer 6)
	
	try
		set afile to (1st file of folder theLocation whose name extension is "pdf")
		set thefile to afile as string
		
	end try
	
	tell application "Adobe Photoshop CS5"
		--set display dialogs to never
		set ruler units of settings to inch units
		open alias thefile
		set mystate to current history state of current document
		resize canvas document 1 width x as inches anchor position middle left
		save current document in file theLocation as JPEG with options {quality:12}
		set current history state of current document to mystate
		resize canvas document 1 width x as inches anchor position middle right
		save current document in file theLocation as JPEG with options {quality:12}
		close current document without saving
		
	end tell
	
	set AppleScript's text item delimiters to oldDelims
end tell

Hi masoodahmad,

Don’t include Tell application “Photoshop CS5” inside another tell block (Finder)

because you want drag and drop file over small app you can do this:

on open myItems
	set pathPDF to (item 1 of myItems) as string
	tell application "Finder"
		set pathParentFolder to (folder of item pathPDF) as string
	end tell
	set pathFrontCover to pathParentFolder & "FrontCover.jpg" -- use the var pathFrontCover to save the file inside Tell Photoshop
	set pathBackCover to pathParentFolder & "BackCover.jpg" -- use the var pathBackCover to save the file inside Tell Photoshop
end open

And automatically you solve the problem of override of final file.

To avoid manual input of width of cover you can insert the value in file name in your workflow and extract it using AppleScript.

Stefano - Ame

Hi Stefan/Ame,

With the help of your guidance and support from Google, I finally created one script for Photoshop CS5. The script looks at the current folder containing the PDF file, crop it with user input size and then creates Front and Back Cover JPEGs with new name.

The script took two full days of mine. Now I’m facing two problems:

First, I’m unable to create a droplet for it i.e. drag and drop as Stefan suggested me

Second, When I switched to Adobe Photoshop CC, the script is giving error.

Can anyone now solve the issues, PLEASE

my Script for Photoshop CS5:


tell application "Finder"
	set theLocation to (target of front window) as string
	set x to text returned of (display dialog "Enter the page width (in inches)" default answer 6)
	
	try
		set afile to (1st file of folder theLocation whose name extension is "pdf")
		set thefile to afile as string
		
	end try
	
	tell application "Adobe Photoshop CS5"
		set display dialogs to never
		set myOptions to {class:PDF open options, mode:RGB, resolution:300, use antialias:true, page:1, constrain proportions:true, crop page:bounding box}
		open file thefile as PDF with options myOptions
		set mystate to current history state of current document
		
		set docRef to current document
		set docName to name of docRef
		set BackC to (theLocation as string) & docName & "_Back Cover"
		set FrontC to (theLocation as string) & docName & "_Front Cover"
		
		resize canvas document 1 width x as inches anchor position middle left
		save docRef in file BackC as JPEG with options {quality:12} with copying
		set current history state of current document to mystate
		
		resize canvas document 1 width x as inches anchor position middle right
		save docRef in file FrontC as JPEG with options {quality:12} with copying
		close docRef without saving
		
	end tell
end tell

Hi,

CC require that save class must be defined so add these line

set jpgOptions to {class:JPEG save options, embed color profile:true, format options:standard, quality:12}
save current document in file BackC as JPEG with options jpgOptions with copying

Read my previous post: is better to not have nested tell. Close Tell Finder before call Tell appl Photoshop.

About droplet read again my post about on open myItems…

Stefano - Ame

this is the droplet version (untested)


on open theItems
	-- set pageWidth to text returned of (display dialog "Enter the page width (in inches)" default answer 6)
	repeat with anItem in theItems
		tell application "System Events"
			set {name extension:fileExtension, container:parentFolder} to anItem
			set parentFolder to path of parentFolder
		end tell
		if fileExtension is "pdf" then
			set pageWidth to text returned of (display dialog "Enter the page width (in inches)" default answer 6)
			
			tell application "Adobe Photoshop CS5"
				set display dialogs to never
				set myOptions to {class:PDF open options, mode:RGB, resolution:300, use antialias:true, page:1, constrain proportions:true, crop page:bounding box}
				
				open file (anItem as text) as PDF with options myOptions
				set mystate to current history state of current document
				
				set docRef to current document
				set docName to name of docRef
				set BackC to parentFolder & docName & "_Back Cover"
				set FrontC to parentFolder & docName & "_Front Cover"
				
				resize canvas document 1 width pageWidth as inches anchor position middle left
				save docRef in file BackC as JPEG with options {quality:12} with copying
				set current history state of current document to mystate
				
				resize canvas document 1 width pageWidth as inches anchor position middle right
				save docRef in file FrontC as JPEG with options {quality:12} with copying
				close docRef without saving
				
			end tell
		end if
	end repeat
end open

At the moment the dialog to enter the page width appears for each page.
If you want it only once remove the comment dashes in the second line and comment out the appropriate line later in the script

Thanks both of you :slight_smile:

@Ame, I got an error after making changes to it in CS5 and CC as well:

"General Photoshop error occurred. This functionality may not be available in this version of Photoshop. - The parameters for command “Save” are not currently valid.

@Stefan,

While testing the droplet in CS5, I got the same error:

"General Photoshop error occurred. This functionality may not be available in this version of Photoshop. - The parameters for command “Save” are not currently valid.

However, after making changes as per Ame, the script worked in CC, but now the output is very weird. The JPG files have the canvas size of 216.537 inches with an input value of 5.5 inches and 236.22 inches with an input value of 6 inches. I can’t put my head with this new problem.

My PDF for testing:
https://www.hightail.com/download/ZUczMWZONEhtUUZwdmNUQw

Output through Droplet in CC:
https://www.hightail.com/download/ZUczMWZONEgwZ24wZXNUQw
https://www.hightail.com/download/ZUczMWZONEh1Yk9FQk1UQw

I think this is our milestone, we are just near to our target…

The updated droplet for CC



on open theItems
	-- set pageWidth to text returned of (display dialog "Enter the page width (in inches)" default answer 6)
	repeat with anItem in theItems
		tell application "System Events"
			set {name extension:fileExtension, container:parentFolder} to anItem
			set parentFolder to path of parentFolder
		end tell
		if fileExtension is "pdf" then
			set pageWidth to text returned of (display dialog "Enter the page width (in inches)" default answer 6)
			
			tell application "Adobe Photoshop CS5"
				set display dialogs to never
				set myOptions to {class:PDF open options, mode:RGB, resolution:300, use antialias:true, page:1, constrain proportions:true, crop page:bounding box}
				
				open file (anItem as text) as PDF with options myOptions
				set mystate to current history state of current document
				
				set docRef to current document
				set docName to name of docRef
				set BackC to parentFolder & docName & "_Back Cover"
				set FrontC to parentFolder & docName & "_Front Cover"
				
				resize canvas document 1 width pageWidth as inches anchor position middle left
				set jpgOptions to {class:JPEG save options, embed color profile:true, format options:standard, quality:12}
				save docRef in file BackC as JPEG with options jpgOptions with copying
				set current history state of current document to mystate
				
				resize canvas document 1 width pageWidth as inches anchor position middle right
				set jpgOptions to {class:JPEG save options, embed color profile:true, format options:standard, quality:12}
				save docRef in file FrontC as JPEG with options jpgOptions with copying
				close docRef without saving
				
			end tell
		end if
	end repeat
end open


Hi masoodahmad,

Add this line:

set pageWidth to pageWidth as real

after:

set pageWidth to text returned of (display dialog “Enter the page width (in inches)” default answer 6)

All will works with both integer value ( example 6) or decimal (5.5)

Stefano - Ame

Love you Ame. Million thanks for a quick workaround. The script is working fine in both CS5 and CC.

@Stefan, Thanks to you too. Without your help this would not have been possible.

This Forum has again proved that this is the best form for Scripting. I feel proud to be a member of this great family.

Once again thanks, thanks thanks… thanks.

Hi Ame,

After a hard labour, I have now created my full script (merging smaller ones in one), which do all my work. I also tried to merge the PDF2Jpg script to my full script, but I’m getting an error:

At this error the script stops and no JPEGs are produced.

I added the PDF2JPG at the end of my final script as this is the second last process of my routine.

Here is the PDF2JPG script which is working fine if run alone:


tell application "Finder"
	--set theLocation to (target of front window) as string
	set pageWidth to text returned of (display dialog "Enter the page width (in inches)" default answer 6)
	set pageWidth to pageWidth as real
	
	try
		set afile to (1st file of folder HighF whose name extension is "pdf")
		set thefile to afile as string
		
	end try
	
	tell application "Adobe Photoshop CS5"
		set display dialogs to never
		set myOptions to {class:PDF open options, mode:CMYK, resolution:300, use antialias:true, page:1, constrain proportions:true, crop page:bounding box}
		open file thefile as PDF with options myOptions
		set mystate to current history state of current document
		
		set docRef to current document
		set docName to name of docRef
		set BackC to JpgFolder & docName & "_Back Cover"
		set FrontC to JpgFolder & docName & "_Front Cover"
		
		resize canvas document 1 width pageWidth as inches anchor position middle left
		set jpgOptions to {class:JPEG save options, embed color profile:true, format options:standard, quality:12}
		save docRef in file BackC as JPEG with options jpgOptions with copying
		set current history state of current document to mystate
		
		resize canvas document 1 width pageWidth as inches anchor position middle right
		set jpgOptions to {class:JPEG save options, embed color profile:true, format options:standard, quality:12}
		save docRef in file FrontC as JPEG with options jpgOptions with copying
		close docRef without saving
		
		
	end tell
end tell

My Final script containing the PDF2JPG at the end:


tell application "Adobe InDesign CS5"
	set myDocument to active document
	set docName to name of myDocument
	set sourceName to text 1 thru -6 of docName
	set sourcePath to the file path of myDocument as string
	set zeroPoint to zero point of active document
	set zero point of active document to {0, 0}
	
	
	tell application "Finder"
		if not (exists folder "1 Native Cover") then set PackageF to make new folder at sourcePath with properties {name:"1 Native Cover"}
		
		if not (exists folder "2 Hi-Res JPEG") then set HighF to make new folder at sourcePath with properties {name:"2 Hi-Res JPEG"}
		set JpgFolder to "Hi-Res JPEG_" & sourceName
		make new folder at HighF with properties {name:JpgFolder}
		
		if not (exists folder "3 Cover Proof") then set LowF to make new folder at sourcePath with properties {name:"3 Cover Proof"}
		if not (exists folder "4 Prepress Cover") then set CanvasF to make new folder at sourcePath with properties {name:"4 Prepress Cover"}
		if not (exists folder "5 Previous Cover") then make new folder at sourcePath with properties {name:"5 Previous Cover"}
		--if not (exists folder "Working Folder") then set WorkF to make new folder at sourcePath with properties {name:"Working Folder"}
		
		set packFolder to make new folder at PackageF with properties {name:sourceName}
	end tell
	
	tell active document
		save myDocument
		package to alias (packFolder as string) copying fonts yes copying linked graphics yes including hidden layers yes copying profiles no updating graphics no ignore preflight errors yes creating report no
		
		set idmlFile to (packFolder as string) & sourceName & ".idml"
		export myDocument format InDesign markup to file idmlFile
		
		--Creating a zip file of the package folder and deleting the folder
		tell application "Finder"
			set theItem to packFolder as alias
			set itemPath to quoted form of POSIX path of theItem
			--set fileName to name of theItem
			set theFolder to POSIX path of (container of theItem as alias)
			set zipFile to quoted form of (theFolder & sourceName & ".zip")
			do shell script "cd " & itemPath & ";zip -r " & zipFile & " *"
			
			if (exists folder theItem) then delete folder theItem
		end tell
		
		set HighPDF to (HighF as string) & sourceName & ".pdf" as string
		export format PDF type to HighPDF using "CSP_EXPORT_3.20.14" without showing options
		
		set LowPDF to (LowF as string) & sourceName & "_Low-Res.pdf" as string
		export format PDF type to LowPDF using "CSP_EXPORT_3.20.14_150DPI" without showing options
		
		--save myDocument
		
		--Creating a Canvas File
		delete every guide of myDocument
		
		tell document preferences of myDocument
			set page height to "12i"
			set page width to "18i"
			set page orientation to landscape
		end tell
		
		set mySel to page items of myDocument
		--ungroup mySel
		
		set MyNewSel to make group with properties {group items:mySel}
		set {w, x, y, z} to geometric bounds of result
		move MyNewSel by {(18 / 2 - (x + z) / 2), (12 - y)}
		
		set canvasFile to (CanvasF as string) & sourceName & "_with Canvas.indd"
		save myDocument to canvasFile
		
		set CanvasPDF to (CanvasF as string) & sourceName & "_with Canvas.pdf" as string
		export format PDF type to CanvasPDF using "CSP_EXPORT_3.20.14" without showing options
		
		close myDocument
	end tell
end tell


tell application "Finder"
	--set theLocation to (target of front window) as string
	set pageWidth to text returned of (display dialog "Enter the page width (in inches)" default answer 6)
	set pageWidth to pageWidth as real
	
	try
		set afile to (1st file of folder HighF whose name extension is "pdf")
		set thefile to afile as string
		
	end try
	
	tell application "Adobe Photoshop CS5"
		set display dialogs to never
		set myOptions to {class:PDF open options, mode:CMYK, resolution:300, use antialias:true, page:1, constrain proportions:true, crop page:bounding box}
		open file thefile as PDF with options myOptions
		set mystate to current history state of current document
		
		set docRef to current document
		set docName to name of docRef
		set BackC to JpgFolder & docName & "_Back Cover"
		set FrontC to JpgFolder & docName & "_Front Cover"
		
		resize canvas document 1 width pageWidth as inches anchor position middle left
		set jpgOptions to {class:JPEG save options, embed color profile:true, format options:standard, quality:12}
		save docRef in file BackC as JPEG with options jpgOptions with copying
		set current history state of current document to mystate
		
		resize canvas document 1 width pageWidth as inches anchor position middle right
		set jpgOptions to {class:JPEG save options, embed color profile:true, format options:standard, quality:12}
		save docRef in file FrontC as JPEG with options jpgOptions with copying
		close docRef without saving
		
		
	end tell
end tell

Could you please suggest me how to rectify the error.

obviously an error occurs in the first line after the try statement.
Catch the error and display the error message


	try
		set afile to (1st file of folder HighF whose name extension is "pdf")
		set thefile to afile as string
	on error e
		display dialog e
		return
	end try

Hi Stefan,

The error message is:

Can you analyze and rectify the possible cause.

The screenshot of the Output folder is also attached for your further reference:
https://www.hightail.com/download/ZUcxSXR5VnNiR0lQWWNUQw

you’ll see that the “2 Hi-Res JPEG” is already there with a child folder “Hi-Res JPEG_Native Cover_123456”