Handlers

Joined
May 30, 2008
Messages
8
Reaction score
0
Points
1
Your Mac's Specs
Intel 10.5.8
The script below works but I know is disastrous because I'm repeating same code. I need help to streamline (fine tune) the scrpit. Based in what I read an handler could help me to organize but i don't know how to implement it.

I want to be able to choose from 2 different types PDF presets (with layers & a composite). Based on the chosen selection then xport pdf. Also I want to chose a folder w ID files (if no ID files found then display a dialog) & want to be able select to click & select ID files on the same folder (chose file & chose folder). Missing fonts, links which will make the script stop. Then xport/ close no save.
Thank you in advance for any advise/help you can give me.

Code:
try
	with timeout of 3600 seconds --1HR
		set source_folder to choose folder with prompt "Select folder containing Indesign Documents to batch as PDF"
		tell application "Finder" to set theFiles to files of source_folder whose name extension is "indd"
		
		--Chosen  folder must be ID files. 
		if theFiles whose name extension is not "indd" then
			display alert "No 'InDesign' files to open?" giving up after 2
		end if
		
		
		set the_choice to display dialog "What PDF you need" buttons {"Quit, I change my mind", "Composite PDF", "Layer PDF"} default button 1 cancel button "Quit, I change my mind"
		
		
		
		if the button returned of the_choice is "Cancel" then
			error number -128
			beep 4
			
			--display dialog " " with icon caution buttons {"Cancel"}
			--end if
			
		else if the button returned of the_choice is "Composite PDF" then
			repeat with oneFile in theFiles
				tell application "Adobe InDesign CS4"
					set user interaction level of script preferences to never interact
					activate
					set myDocument to open (oneFile as alias)
					tell myDocument
						---------------
						
						
						--++++++++++++++++
						--Check links
						if (count of ((links whose status is not normal) as list)) > 0 then
							--with timeout of 9999 seconds
							display dialog "Batch will Stop. Please update all MODIFIED or MISSING Links." buttons {"OK"} with icon 1 default button 1 cancel button 1 giving up after 300 --5min
							--end timeout
							return
						end if
						
						--+++++++++++++++++
						--Check if there are any text boxes with overset text
						set thereIsTextOverflow to ((overflows of parent story of every text frame) contains true)
						if thereIsTextOverflow then
							--with timeout of 9999 seconds
							display dialog "Batch will Stop. Please FIX Overflow Text Boxes. " buttons {"OK"} with icon 1 default button 1 cancel button 1 giving up after 300 --5min
							--end timeout
						end if
						
						--++++++++++++++++++++++
						--check missing fonts
						set myfontprop to properties of every font
						set font_list to {}
						repeat with i from 1 to the number of myfontprop
							set this_font_item to item i of myfontprop
							set myfontname to name of this_font_item as string
							set fontstatus to status of this_font_item as string
							set font_list to font_list & fontstatus
						end repeat
						if font_list contains "not available" then
							display dialog "Batch will Stop. A Font is missing." buttons {"OK"} with icon 1 default button 1 cancel button 1 giving up after 300 --5min
						end if
						
						if font_list contains "substituted" then
							
							display dialog "Batch will Stop. A Font has been substituted/missing." buttons {"OK"} with icon 1 default button 1 cancel button 1 giving up after 300 --5min
						end if
						--+++++++++++++++++++++++++++
						
						set source_folder to file path of myDocument
						set theName to name of myDocument
						set text item delimiters of AppleScript to {"_"}
						set theShortName to text 1 thru text item -2 of theName
						--text 1 thru text item -2 is every character from the first character to the second to last text item
						set text item delimiters of AppleScript to ""
						
						tell application "Finder"
							if (exists folder "PDFs" of folder source_folder) is false then
								make folder at source_folder with properties {name:"PDFs"}
							end if
						end tell
						
						tell application "Adobe InDesign CS4"
							repeat with x from 1 to count pages of myDocument
								
								set thePageName to name of page x of myDocument
								set page range of PDF export preferences to thePageName
								
								--set theFilePath to source_folder & "PDFs:" & theShortName & "_" & thePageName & ".pdf" as string
								set threeDigitPageName to text -3 thru -1 of ("00" & thePageName)
								(* text 1 thru 3 are the first 3 characters 
					text -3 thru -1 are the last 3 characters*)
								set theFilePath to source_folder & "PDFs:" & theShortName & "_" & threeDigitPageName & ".pdf" as string
								
								--EXPORT PDF
								tell myDocument
									with timeout of 600 seconds --10mins
										export format PDF type to theFilePath using "PressQuality_Crops" without showing options
										--export format PDF type to theFilePath using "PressTest" without showing options
									end timeout
								end tell
								
							end repeat
						end tell
						close myDocument saving no
						
						--========================================
						
						
						--end tell
						----------------
					end tell
					set user interaction level of script preferences to interact with all
					
				end tell
			end repeat
			
			
			
		end if
		
		
		--=================================================
		if the button returned of the_choice is "Layer PDF" then
			repeat with oneFile in theFiles
				tell application "Adobe InDesign CS4"
					set user interaction level of script preferences to never interact
					activate
					set myDocument to open (oneFile as alias)
					tell myDocument
						---------------
						
						
						--++++++++++++++++
						--Check links
						if (count of ((links whose status is not normal) as list)) > 0 then
							--with timeout of 9999 seconds
							display dialog "Batch will Stop. Please update all MODIFIED or MISSING Links." buttons {"OK"} with icon 1 default button 1 cancel button 1 giving up after 300 --5min
							--end timeout
							return
						end if
						
						--+++++++++++++++++
						--Check if there are any text boxes with overset text
						set thereIsTextOverflow to ((overflows of parent story of every text frame) contains true)
						if thereIsTextOverflow then
							--with timeout of 9999 seconds
							display dialog "Batch will Stop. Please FIX Overflow Text Boxes. " buttons {"OK"} with icon 1 default button 1 cancel button 1 giving up after 300 --5min
							--end timeout
						end if
						
						--++++++++++++++++++++++
						--check missing fonts
						set myfontprop to properties of every font
						set font_list to {}
						repeat with i from 1 to the number of myfontprop
							set this_font_item to item i of myfontprop
							set myfontname to name of this_font_item as string
							set fontstatus to status of this_font_item as string
							set font_list to font_list & fontstatus
						end repeat
						if font_list contains "not available" then
							display dialog "Batch will Stop. A Font is missing." buttons {"OK"} with icon 1 default button 1 cancel button 1 giving up after 300 --5min
						end if
						--+++++++++++++++++++++++++++
						
						set source_folder to file path of myDocument
						set theName to name of myDocument
						set text item delimiters of AppleScript to {"_"}
						set theShortName to text 1 thru text item -2 of theName
						--text 1 thru text item -2 is every character from the first character to the second to last text item
						set text item delimiters of AppleScript to ""
						
						tell application "Finder"
							if (exists folder "PDFs" of folder source_folder) is false then
								make folder at source_folder with properties {name:"PDFs"}
							end if
						end tell
						
						tell application "Adobe InDesign CS4"
							repeat with x from 1 to count pages of myDocument
								
								set thePageName to name of page x of myDocument
								set page range of PDF export preferences to thePageName
								
								--set theFilePath to source_folder & "PDFs:" & theShortName & "_" & thePageName & ".pdf" as string
								set threeDigitPageName to text -3 thru -1 of ("00" & thePageName)
								(* text 1 thru 3 are the first 3 characters 
					text -3 thru -1 are the last 3 characters*)
								set theFilePath to source_folder & "PDFs:" & theShortName & "_" & threeDigitPageName & ".pdf" as string
								
								--EXPORT PDF
								tell myDocument
									with timeout of 600 seconds --10mins
										export format PDF type to theFilePath using "PressQuality_LAYERS" without showing options
										--export format PDF type to theFilePath using "PressTest" without showing options
									end timeout
								end tell
								
							end repeat
						end tell
						close myDocument saving no
						
						--========================================
						
						
						--end tell
						----------------
					end tell
					set user interaction level of script preferences to interact with all
					
				end tell
			end repeat
			
			
			
		end if
	end timeout
	
	
on error number -1712 -- the comand time out, so cancel the script
	tell application "System Events"
		tell application process "Finder"
			click button "Cancel" of window "Choose a Folder"
		end tell
	end tell
	error number -128 --throw a user cancelled error
end try

beep 5
with timeout of 10000 seconds
	tell application "Finder"
		activate
		display dialog "Check the PDFs " buttons {"OK"} with icon 1 default button 1 giving up after 15
	end tell
end timeout
 

chscag

Well-known member
Staff member
Admin
Joined
Jan 23, 2008
Messages
65,248
Reaction score
1,833
Points
113
Location
Keller, Texas
Your Mac's Specs
2017 27" iMac, 10.5" iPad Pro, iPhone 8, iPhone 11, iPhone 12 Mini, Numerous iPods, Monterey
Post was moved here to the proper forum. It does not belong in Community Suggestions and Feedback. Please observe forum descriptions before posting.
 
OP
N
Joined
May 30, 2008
Messages
8
Reaction score
0
Points
1
Your Mac's Specs
Intel 10.5.8
where is this post was moved I can't find it
 

pigoo3

Well-known member
Staff member
Admin
Joined
May 20, 2008
Messages
44,212
Reaction score
1,424
Points
113
Location
U.S.
Your Mac's Specs
2017 15" MBP, 16gig ram, 1TB SSD, OS 10.15

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top