Fine tune

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.

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

If it was me I would make a handler for each part that you had between the --++++++++++++++++++++++++.
So then the main part of your script would look something like:


tell application "Adobe Indesign CS4"
my checkLinks()
my checkOverset()
my checkMissingFonts()
-- etc
-- etc
end tell

Then, the checkLinks() handler would be:


on checkLinks()
	tell application "Adobe InDesign CS4"
		if (count of ((links whose status is not normal) as list)) > 0 then
			display dialog "Batch will Stop. Please update all MODIFIED or MISSING Links." buttons {"Cancel"} default button "Cancel" giving up after 300 --5min
		end if
	end tell
end checkLinks

I think the only difference between the two lengthy sections of code is the PDF setting? If so you could further tidy things up by setting the variable with your first dialog box:


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 button returned of the_choice is "Composite PDF" then
	set myPDFpreset to "PressQuality_Crops"
else if button returned of the_choice is "Layer PDF" then
	set myPDFpreset to "PressQuality_LAYERS"
else
	error number -128
end if

Then you can have the same code all the way through with the export line as this:


export format PDF type to theFilePath using myPDFpreset without showing options

at the beginning of your script you might need to put

property myPDFpreset : missing value 

Hope this helps

Thank you divster for your reply. As can see you I’m not an expert & not even close to be one. i just know few basics patching scripts. Your suggestions are helping me to understand more.
The part that check links is not letting the script to continue to do the Export which i understand why is happening but i tried to put it in a different location but no success. Do you think you can help me once gain?

Another question? how do you check if the chosen folder contains ID files?


if theFiles whose name extension is not "indd" then
			display alert "No 'InDesign' files to process" giving up after 2
		end if

Thanks again.

property myPDFpreset : missing value
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"


set the_choice to display dialog "What PDF you need" buttons {"Quit, I changed my mind", "Composite PDF", "Layer PDF"} default button 1 cancel button "Quit, I changed my mind"
if button returned of the_choice is "Composite PDF" then
	set myPDFpreset to "PressQuality_Crops"
else if button returned of the_choice is "Layer PDF" then
	set myPDFpreset to "PressQuality_LAYERS"
else
	error number -128
end if

--Where I should I put this so it doesn't get called that quickly"
tell application "Adobe InDesign CS4"
	my checkLinks()
	my checkOverset()
	my checkMissingFonts()
	
	
	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
				--==
				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 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 myPDFpreset without showing options
							end timeout
						end tell
						
					end repeat
				end tell
				close myDocument saving no
				
				
				set user interaction level of script preferences to interact with all
				
				
				
				--===
			end tell
		end tell
	end repeat
	
	
end tell
--+++++++++++++++++
--Check links
on checkLinks()
	tell application "Adobe InDesign CS4"
		if (count of ((links whose status is not normal) as list)) > 0 then
			display dialog "Batch will Stop. Please update all MODIFIED or MISSING Links." buttons {"Cancel"} default button "Cancel" giving up after 300 --5min
			return
		end if
	end tell
end checkLinks

--Check overset text
on checkOverset()
	tell application "Adobe InDesign CS4"
		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
	end tell
end checkOverset

--check missing fonts
on checkMissingFonts()
	tell application "Adobe InDesign CS4"
		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
		else if font_list contains "substituted" then
			display dialog "Batch will Stop. A Font has been substituted." buttons {"OK"} with icon 1 default button 1 cancel button 1 giving up after 300 --5min
		end if
	end tell
end checkMissingFonts
--+++++++++++++++++

Your checkLinks() handler doesn’t work for two reasons. The first is that there is no document for it to check because it has not been opened! So put those handlers after the line:

set myDocument to open (oneFile as alias)

Then I think you need to include another tell statement in the handler:

on checkLinks()
	tell application "Adobe InDesign CC"
		tell myDocument
		if (count of ((links whose status is not normal) as list)) > 0 then
			display dialog "Batch will Stop. Please update all MODIFIED or MISSING Links." buttons {"Cancel"} default button "Cancel" giving up after 300 --5min
			return
		end if
	end tell
end tell
end checkLinks

So now you are addressing the document that you have just opened. For this to work you will need declare myDocument as a property at the beginning of the script. Otherwise the handler doesn’t understand it. Or you could pass the variable to the handler, but that’s a bit more fiddly.

You now have an unnecessary “tell myDocument” in your main script just after setting it on open. All the handlers will have it in and you have already addressed myDocument in the following lines:

set source_folder to file path of myDocument
set theName to name of myDocument

To stop the script if there are no files then do this after the first tell Finder statement:

if (count of theFiles) is 0 then
	display dialog "No Indesign files to process" buttons "Cancel" default button "Cancel" giving up after 2
	return
end if

You’ve already defined the files as those with the extension .indd

Wonderful Thank you!