Indesign close file, create log (if errors) and continue with the next file

The script that follows produces PDFs. My plan is to let the script make PDFs; if there are errors, such as missing fonts, updated links, missing links, or overset text boxes, it will close the ID file, it will create report log file with the name of file & the type of error, and then continue making PDFs. Making this happen is proving to be challenging. For instance, if I select three ID files with missing fonts, modified links, or overset boxes, and the first one has missing links, the script will proceed with the modified links block, but the file will close and I will receive an execution error since the file is no longer open. I rely heavily on your expertise and whatever assistance you may provide. Thanks

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 "Flatten PDF w/o Crop marks"
if the_choiceFav is false then error number -128 (* user cancelled *)
set myPDFpreset to item 1 of the_choiceFav

-- select region
set the_PROOF_choice to {"south", "north", "west", "east", "se", "ne"}
set the_PROOF_choiceFav to choose from list the_PROOF_choice with title "Regions" with prompt "Select a region to add the name to the PDFs" default items (item 1 of the_PROOF_choice)
if the_PROOF_choiceFav is false then error number -128 -- user cancelled
---_______________________
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 activeDoc to true
			
			set DocName to name -- this is for the error log file
			
			--Check 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
				my resetUserInteractionLevel()
				say "Missing links found." using "Samantha"
				display alert "Missing Links found." buttons {"See Batch Errors Log.txt file on your desktop"} as critical cancel button 1 giving up after 2 --120 --2mins
				set TheText to DocName & {tab} & "MISSING links" & return
				my write_to_file(TheText, true)
				set activeDoc to false
				close myDocument saving no
				--error number -128
			else if (count of ((links whose status contains link out of date) as list)) > 0 then
				my resetUserInteractionLevel()
				say "Update Modified links." using "Samantha"
				display alert "Update Modified Links." buttons {"See Batch Errors Log.txt file on your desktop"} as critical cancel button 1 giving up after 2 --120 --2mins
				set TheText to DocName & {tab} & "Modified links" & return
				my write_to_file(TheText, true)
				set activeDoc to false
				close myDocument saving no
				--error number -128
			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
				my resetUserInteractionLevel()
				say "Overset text boxes found." using "Samantha"
				display alert "Overset text boxes found.
					TIP: Turn on hidden layers and look for overset text boxes." buttons {"OK"} as warning default button 1 giving up after 2 --10secs
				set TheText to DocName & {tab} & "Overset text boxes" & return
				my write_to_file(TheText, true)
				set activeDoc to false
				
				close myDocument saving no
				
				--error number -128 --If user select the button "See Batch Errors Log.txt file on your desktop" it will cancel pero si continua y se expira los segs. hay que poner este error para cancelar el export 
			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()
				--display dialog " Missing Font." buttons {"OK"} with icon 0 default button 1 cancel button 1 giving up after 120 --2mins
				say "Missing Fonts found." using "Samantha"
				display alert "Missing Fonts found." buttons {"See Batch Errors Log.txt file on your desktop"} as critical cancel button 1 giving up after 5
				set TheText to DocName & {tab} & "Fonts missing" & return
				my write_to_file(TheText, true)
				set activeDoc to false
				close myDocument saving no
				
				--error number -128
			else if font_list contains substituted then
				my resetUserInteractionLevel()
				--display dialog "A Font has been substituted or it is missing." buttons {"OK"} with icon 0 default button 1 cancel button 1 giving up after 300 --5min
				say "A Font has been substituted or it is missing." using "Samantha"
				display alert "A Font has been substituted or it is missing." buttons {"See Batch Errors Log.txt file on your desktop"} as critical cancel button 1 giving up after 5
				
				set TheText to DocName & {tab} & "Font substituted or it is missing" & return
				my write_to_file(TheText, true)
				set activeDoc to false
				set dateFormatter to NSDateFormatter's alloc()'s init()
				(dateFormatter's setDateFormat:"YY-dd-MM hh:mm:ss")
				close myDocument saving no
				
				error number -128
			end if
			
			---_______________________
		end 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 "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
				
				--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
		end tell
		close myDocument saving no
		
		my resetUserInteractionLevel()
		--====
		
	end tell
end repeat

on setUserInteractionLevel()
	tell application id "com.adobe.indesign"
		set user interaction level of script preferences to never interact
	end tell
end setUserInteractionLevel

on resetUserInteractionLevel()
	tell application id "com.adobe.indesign"
		set user interaction level of script preferences to interact with all
	end tell
end resetUserInteractionLevel


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

Maybe I’m missing something, but you already identified the problem in your description:

but the file will close and I will receive an execution error since the file is no longer open

That’s because you tell it to:

			if (count of ((links whose status contains link missing) as list)) > 0 then
--- ...functional code here
				close myDocument saving no

So you’re directly and specifically closing the doc at the any error.

The solution, I think, is simple - don’t close the file.

You should have a single ‘close myDocument…’ line at the end of the repeat loop…

repeat:
open a file
look for link errors - log it
look for overset errors - log it, too
look for font errors - log it, too
all done looking for errors? close the file
end repeat

Hello @Camelot ,
The problem is that the script creates a PDF when there is an error caused by missing fonts, links, modified links, or overset, which is not what I need to happen. That’s why I added the 'close myDocument… line, but because the file is no longer open, I obviously get an execution error. I should have not included that line. I’m not a professional scripter, so I’m on a learning curve here.
I do not want a PDF to be exported, If there are errors, I would like it to capture the Indesign file name and the error in the log text file, close the Indesign document, and continue on to the next Indesign document. I’ve been attempting to do this for days, but I’ve been unsuccessful since I don’t know anyone who is familiar with AppleScript, therefore, I must rely entirely on the experts on this website.

I don’t have easy access to InDesign on this machine to fully edit and test, but, rather than the audio feedback and document closure you’ve implemented, you need to set a variable to act as a gatekeeper for the export action commencement; pseudocode follows:

repeat with oneFile in theFiles
	tell application id "com.adobe.indesign"
tell (open oneFile)
		set {Errata,linkProbs,flowOver,fontStat} to {false,false,false,false}
		--find problems
		if (count of ((links whose status contains link missing) as list)) > 0 then set {errata, linkProbs} to {true, true}
		if ((overflows of parent story of every text frame) contains true) then {errata, flowOver} to {true, true}
		if (get status of every font) contains not available then then {errata, fontStat} to {true, true}

					if errata is not true then
		export format PDF type to theFilePath using myPDFpreset without showing options
		else
		--write {name, return,  linkProbs, return, flowOver, return, fontStat} as text to theFilePath --log problems
		end 
		close oneFile
				end tell 
	--continue the loop to next file
	end repeat

@Marc_Anthony
Hi Marc. I tried to put your advice into practice, but I must still be doing something wrong. For now, I’m not going to use the find missing or modified links blocks until I get the script to work the way I want it to, and then I’ll add them and include the log text file. I added the handler
on setExportPDF(myDocument, theFilePath) to export the PDF in order to prevent having to repeat the same code throughout the script . However, by doing so, I have introduced a new issue. I now receive an error stating that the variable theFilePath is not defined. In essence, I am no longer able to export a pdf if files are OK without errors, and if they have errors, avoid the export and log the errors. Could you help me to correct my errors?

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 "Flatten PDF w/o Crop marks"
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 ---myDocument is the InDesign file that needs to be exported 
			set DocName to name -- this is for the error log file
			set {Errata, MissLinks, ModifiedLinks, OverSetTx, FontMiss, SubFont} to {false, false, false, false, false, false} --find Missing Links, Modified links, Overset txt, Fonts missing, Fonts substituted 
			
			
			--Check 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
				my resetUserInteractionLevel()
				set {Errata, MissLinks} to {true, true}
				display alert "Missing Links found." buttons {"See Batch Errors Log.txt file on your desktop"} as critical cancel button 1 giving up after 2 --120 --2mins
			else if (count of ((links whose status contains link out of date) as list)) > 0 then
				my resetUserInteractionLevel()
				set {Errata, 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 --120 --2mins
			else if Errata is not true then
				my setExportPDF(myDocument, theFilePath)
			end if
			
		end tell --my Document
	end tell --app id
end repeat --w oneFile


on setExportPDF(myDocument, theFilePath) --& theFilepath the path where the exported PDF file should be saved
	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 "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
end setExportPDF

close myDocument saving no
my resetUserInteractionLevel()

beep 6
----+++++++++++++++

on setUserInteractionLevel()
	tell application id "com.adobe.indesign"
		set user interaction level of script preferences to never interact
	end tell
end setUserInteractionLevel

on resetUserInteractionLevel()
	tell application id "com.adobe.indesign"
		set user interaction level of script preferences to interact with all
	end tell
end resetUserInteractionLevel

Do I need to submit something different? It must be difficult to check someone’s code but I’m stuck with this.