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