Skip to main content
Known Participant
June 29, 2023
Answered

Applescript check errors, close file, and continue with the next Indesign file

  • June 29, 2023
  • 2 replies
  • 1329 views

 I know the basics of Applescript. I need help fixing this script. If there are errors due to missing links, modified links, overset text, or missing or substituted fonts, it needs to close the Indesign file & continue on to the next document but currently the script is creating PDFs for all files with or without errors.  For example if I add a line to close the file (close myDocument saving no) after checking for missing links the script still continues with the other block to check for overset txt, fonts but the file is already closed. I'm stuck, making no progress for over a 3weeks because I just don't know how to do what I want to do.  I really appreciate any help you will be able to provide.

 

---Indesign file names must have minimun 2 underscores ei: xxx_1_000.indd

set source_folder to choose file of type "IDdD" with prompt "Select the InDesign files to export as single PDFs" with multiple selections allowed without invisibles

tell application "Finder" to set theFiles to every item of source_folder

set the_choice to {"Smallest File Size", "High Quality Print", "PDF/X-1a", "Press Quality"}

set the_choiceFav to choose from list the_choice with title "PDF Job Options" with prompt "Select a PDF Job Option" default items "Smallest File Size"

if the_choiceFav is false then error number -128 (* user cancelled *)

set myPDFpreset to item 1 of the_choiceFav

 

 

repeat with oneFile in theFiles

tell application id "com.adobe.indesign"

my setUserInteractionLevel() -- no indesign messages

activate

set myDocument to open (oneFile as alias)

tell myDocument

set DocName to name

set {Errors, missingLinks, ModifiedLinks, OverSetTx, FontsMissing, SubFonts} to {false, false, false, false, false, false} --find Missing Links, Modified links, Overset txt, Fonts missing, Substituted fonts 

set Errors to true

 

--Check Missing links +++++++++++++++++

set allLinks to (every link whose status is link out of date)

if (count of ((links whose status contains link missing) as list)) > 0 then

set {Errors, MissLinks} to {true, true}

my resetUserInteractionLevel()

display alert "Missing Links found." buttons {"See Batch Errors Log.txt file on your desktop"} as critical cancel button 1 giving up after 2

--Check Mofidified links +++++++++++

else if (count of ((links whose status contains link out of date) as list)) > 0 then

my resetUserInteractionLevel()

set {Errors, ModifiedLinks} to {true, true}

display alert "Update Modified Links." buttons {"See Batch Errors Log.txt file on your desktop"} as critical cancel button 1 giving up after 2

 

--else DO NOT CREATE PDF. Close the Indesign file  & continue with the next file

end if

 

--Check  boxes with overset text+++++++++++++++++

set thereIsTextOverflow to overflows of parent story of every text frame contains true --and layer's visible is true --just check visible layers

if thereIsTextOverflow then

my resetUserInteractionLevel()

set {Errors, OverSetTx} to {true, true}

display alert "Overset text boxes found." & return & return & DocName & return & return & "TIP: Turn on hidden layers and look for overset text boxes." & return buttons {"See Batch Errors Log.txt file on your desktop"} as critical cancel button 1 giving up after 3

-- else DO NOT CREATE PDF. Close the Indesign file  & continue with the next file

 

end if

 

--check Missing Fonts++++++++++++++++++++++

set font_list to status of every font -- constants list

if font_list contains not available then -- constant comparsion 

my resetUserInteractionLevel()

set {Errors, FontMiss} to {true, true}

display alert "Missing Fonts found." & return & return & DocName & return buttons {"See Batch Errors Log.txt file on your desktop"} as critical cancel button 1 giving up after 3

--check substituted fonts

else if font_list contains substituted then

my resetUserInteractionLevel()

set {Errors, SubFonts} to {true, true}

display alert "A Font has been substituted or it is missing." & return & return & DocName & return buttons {"See Batch Errors Log.txt file on your desktop"} as critical cancel button 1 giving up after 3

--else DO NOT CREATE PDF. Close the Indesign file  & continue with the next file

end if

 

--start xport

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" --export PDF to this folder

if (exists folder "PDF" of folder source_folder) is false then

make folder at source_folder with properties {name:"PDF"}

end if

end tell

 

tell application id "com.adobe.indesign"

set pagesCount to count pages of myDocument

tell me to set progress total steps to pagesCount --progress total steps. the number of steps needed to complete this task. 

repeat with x from 1 to pagesCount

tell me to set progress description to "Page " & x & " of " & pagesCount --progress description. Text describing this task.

tell me to activate

set thePageName to name of page x of myDocument

set page range of PDF export preferences to thePageName

--section number must have 3 digits

set threeDigitPageName to text -3 thru -1 of ("00" & thePageName)

--text -3 thru -1 are the last 3 characters*)

--set theFilePath to source_folder & "PDF:" & theShortName & "_" & threeDigitPageName & "_" & the_PROOF_choiceFav & ".pdf" as string

set theFilePath to source_folder & "PDF:" & 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

repeat -- repeat until current export is finished

try

delay 1

alias theFilePath

exit repeat

end try

end repeat

end timeout

end tell

tell me to set progress completed steps to x --progress completed steps. The number of steps of the task that have been completed so far.

end repeat -- with x from 1

end tell

 

 

----++++++++++++++++++++

 

close myDocument saving no

my resetUserInteractionLevel() --show ID alerts messages

end tell --my Document

end tell --app id

end repeat --w oneFile

 

----+++++++++++++++

 

on setUserInteractionLevel() --hide Indesign alerts

tell application id "com.adobe.indesign"

set user interaction level of script preferences to never interact

end tell

end setUserInteractionLevel

 

on resetUserInteractionLevel() --show Indesign alerts

tell application id "com.adobe.indesign"

set user interaction level of script preferences to interact with all

end tell

end resetUserInteractionLevel

 

This topic has been closed for replies.
Correct answer rob day

I apologize for not responding sooner. I recently got back from vacation. When I ran your script as is on multiple files on, I got an execution error. It stops on this line

set fPath to PDFfolder & dn & "_" & pn & ".pdf" as string


Offending commnad

Can’t make «class cfol» "PDF" of «class cfol» "test" of «class cfol» "test-a-000" of «class cfol» "Projects" of «class cfol» "Desktop" of «class cfol» "xxx-xxx" of «class cfol» "Users" of «class sdsk» of application "Finder" into type list, record or text.

Expected type list, record or text


We are probably on different OS versions. Try changing

 

set PDFfolder to folder "PDF" of parent of item 1 of fList as alias

 

to:

 

set PDFfolder to folder "PDF" of parent of item 1 of fList as string

2 replies

rob day
Community Expert
June 29, 2023

Not sure if this helps, but the code could be simplified. Yours seems to be intending to output some kind of error file, but there’s no code for that. This checks the documents as they open and if they pass the link, overset, and type test, isExport is set to true. There is an exportDoc handler at the bottom where you can add your export code

 

global myPDFpreset
global fList
tell application "Finder"
	set newImages to choose folder
	set fList to (every file of newImages whose file type is "IDdD") as alias list
	
	set a to {"Smallest File Size", "High Quality Print", "PDF/X-1a", "Press Quality"}
	set myPDFpreset to choose from list a with title "PDF Job Options" with prompt "Select a PDF Job Option" default items "Smallest File Size"
	if myPDFpreset is not false then my getFiles()
end tell

--open the files
on getFiles()
	tell application id "com.adobe.indesign"
		set user interaction level of script preferences to never interact
		repeat with x in fList
			set isExport to true
			set myDocument to open (x as alias)
			tell myDocument
				set dn to name
				
				--get link, overset, and type problems as lists
				set lc to (every link whose status is link missing)
				set os to (every story whose overflows is true)
				set fna to (every font whose status is equal to not available)
				
				--if any of the lists contain an item set the export boolean to false
				if (count of lc) is greater than 0 or (count of os) is greater than 0 or (count of fna) is greater than 0 then
					set isExport to false
				end if
				
				--if the export boolean is true run the export code
				if isExport is true then
					display dialog "Export " & dn
					--run the export handler
					my exportDoc(myDocument)
					close myDocument saving no
				else
					display dialog "Do NotExport " & dn
				end if
			end tell
		end repeat
		set user interaction level of script preferences to interact with all
	end tell
end getFiles



(*
* Handler to Export a document to pdf
* @ param doc, the document to export
* @ return nothing 
*)

on exportDoc(doc)
	tell application id "com.adobe.indesign"
		tell doc
			--get myPDFpreset
			--export code goes here
		end tell
	end tell
end exportDoc

 

nrod2012Author
Known Participant
June 30, 2023

@rob day 

I was able to visualize what you indicated in the prior post thanks to the code, which I then incorporated into the script. I liked how you exceptionally simplified a lot of things. The use of handlers has occasionally proven to be a challenge for me because I occasionally experience compile issues, despite the fact that they are advantageous.

I originally used choose folder at the script's beginning, but I modified it to choose file of type because I like to select individual files rather than a whole folder.
I inserted the error output file code, and now whenever a file with a missing link is produced, an error text file with the name of the file and the cause for the error is created on the desktop, but the script stops and I receive an error. User interaction preferences for the script cannot be set. And if I use it to export a file without errors or to check the fonts, the script tries to export a pdf but fails with the message that the variable x is not specified. I sincerely appreciate any assistance you may provide.. Thank you

---Indesign file names must have minimun 2 underscores ei: xxx_1_000.indd
global myPDFpreset
global theFiles
global myDocument
global theFilePath


set source_folder to choose file of type "IDdD" with prompt "Select the InDesign files to export as single PDFs" with multiple selections allowed without invisibles
--tell application "Finder" to set theFiles to every item of source_folder
tell application "Finder"
	set theFiles to every item of source_folder
	
	set the_choice to {"Smallest File Size", "High Quality Print", "PDF/X-1a", "Press Quality"}
	set the_choiceFav to choose from list the_choice with title "PDF Job Options" with prompt "Select a PDF Job Option" default items "Smallest File Size"
	set myPDFpreset to item 1 of the_choiceFav
	if the_choiceFav is not false then my getFiles()
end tell


on getFiles()
	tell application id "com.adobe.indesign"
		set user interaction level of script preferences to never interact
		-- no indesign messages
		activate
		repeat with oneFile in theFiles
			set myDocument to open (oneFile as alias)
			tell myDocument
				set DocName to name
				set canXport to true
				
				--Check links, Overset, Fonts not available +++++++++++++++++
				set lc to (every link whose status is link missing)
				set os to (every story whose overflows is true)
				set fna to (every font whose status is equal to not available)
				
				--if any of the lists contain an item set the export boolean to false
				(*
					if (count of lc) is greater than 0 or (count of os) is greater than 0 or (count of fna) is greater than 0 then
						set canXport to false
				*)
				if (count of lc) is greater than 0 then
					set canXport to false
					-- the lines below are for the output error file
					set TheText to DocName & {tab} & "missing links" & return
					my write_to_file(TheText, true)
					
				else if (count of os) is greater than 0 then
					set canXport to false
					-- the lines below are the output error file
					set TheText to DocName & {tab} & "overset text" & return
					my write_to_file(TheText, true)
					
				else if (count of fna) is greater than 0 then
					set canXport to false
					-- the lines below are the output error file
					set TheText to DocName & {tab} & "missing fonts" & return
					my write_to_file(TheText, true)
				end if
				
				--if the export boolean is true run the export code
				if canXport is true then -- if ok export 
					--display dialog "Export " & dn with icon 0 default button 1 cancel button 1 giving up after 5
					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" --export PDF to this folder
						if (exists folder "PDF" of folder source_folder) is false then
							make folder at source_folder with properties {name:"PDF"}
						end if
					end tell
					
					tell application id "com.adobe.indesign"
						set pagesCount to count pages of myDocument
						tell me to set progress total steps to pagesCount --progress total steps. the number of steps needed to complete this task. 
						repeat with x from 1 to pagesCount
							tell me to set progress description to "Page " & x & " of " & pagesCount --progress description. Text describing this task.
							tell me to activate
							set thePageName to name of page x of myDocument
							set page range of PDF export preferences to thePageName
							--section number must have 3 digits
							set threeDigitPageName to text -3 thru -1 of ("00" & thePageName)
							--text -3 thru -1 are the last 3 characters*)					
							set theFilePath to source_folder & "PDF:" & theShortName & "_" & threeDigitPageName & "_" & ".pdf" as string
						end repeat -- with x from 1
					end tell
					--run the export handler
					my exportDoc(myDocument)
					close myDocument saving no
					
				else
					display dialog "Do NotExport " & DocName with icon 0 default button 1 cancel button 1 giving up after 5
				end if
				
				
				
				set user interaction level of script preferences to interact with all --show ID alerts messages again
			end tell --my Document
		end repeat --w oneFile
	end tell --app id
	
end getFiles


on exportDoc(doc)
	tell application id "com.adobe.indesign"
		tell myDocument
			with timeout of 600 seconds --10mins
				export format PDF type to theFilePath using myPDFpreset without showing options
				--export format PDF type to theFilePath using myPDFpreset without showing options
				repeat -- repeat until current export is finished
					try
						delay 1
						alias theFilePath
						exit repeat
					end try
				end repeat
			end timeout
		end tell
		tell me to set progress completed steps to x --progress completed steps. The number of steps of the task that have been completed so far.
	end tell
end exportDoc

--- for the output  error file
on write_to_file(this_data, append_data)
	try
		set the log_file to (path to desktop as text) & "Batch Errors Log.txt" as text
		set the open_target_file to open for access file log_file with write permission
		if append_data is false then set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file log_file
		end try
		return false
	end try
end write_to_file

beep 6

 

nrod2012Author
Known Participant
June 30, 2023
rob day
Community Expert
June 29, 2023

Hi @nrod2012 , I don’t have files that I can test your script with, but you are setting a list of error booleans with this line:

 

set {Errors, missingLinks, ModifiedLinks, OverSetTx, FontsMissing, SubFonts} to {false, false, false, false, false, false}

 

and when you encounter errors you are setting some to true, but then you never actually use them in the script. You export the document without checking if the flags you setup earlier have been set to true or false, so all the documents get exported. Not sure why you need 6 true/false variables, seems like it could be one that starts as true—e.g., set canPrint to true—and gets set to false if any of the don’t export conditions are met. Then:

 

if canPrint is true then
--EXPORT PDF CODE
end if