Fixing Bug that Repeats Dialogs When It Shouldn't

I’m noticing something weird in several of my scripts. If I have a display dialog before the first repeat loop, the script will display that dialog multiple times even though it should only display the dialog once. I put a test to see how many files were actually in the dropped file count. Instead of displaying six, like it should have, the script displayed “1”. Then it popped up a second dialog counting “5” dropped files. Any ideas on how to fix this would be greatly appreciated!

on open droppedfiles
	
	--Testing how many items were dropped on the script
	display dialog "Hey! you dropped " & (droppedfiles's length) & " item(s) on me!" --> Should return 6. Instead returns 1, then returns 5
	
	
	--Prompting the user to name the combined pdf file
	display dialog "Type the name of the combined pdf (don't type in the '.pdf'): " default answer "Example: 4th of July Spec Ad"
	set the_name to text returned of the result
	
	
	--Suppressing interrupting dialogs in InDesign
	tell application "Adobe InDesign CC 2017"
		activate
		set user interaction level of script preferences to never interact 
	
	
	--Exporting a pdf
	repeat with afile in droppedfiles
		tell application "Adobe InDesign CC 2017"
			
			open afile
			tell active document
				export format PDF type to ("Users:mbornbach:Desktop:Desktop Process:Test Scripts:PDFs:" & the_name & ".pdf" as string) without showing options
			end tell
			close active document saving no
		end tell
	end repeat
	
end open

Problem already described and treated in applescript-users@lists.apple.com

AppleScript pass two groups of files :
those whose quarantine bit is set and those whose bit is not set.

Shane STANLEY posted :

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property filesToOpen : {}

on open fileList
	set my filesToOpen to my filesToOpen & fileList
	-- cancel any pending performSelector: requests
	current application's NSObject's cancelPreviousPerformRequestsWithTarget:me
	-- handle files after a short delay in case further events are received
	tell me to performSelector:"doOpen" withObject:(missing value) afterDelay:0.5
end open

on doOpen()
	copy my filesToOpen to fileList
	set my filesToOpen to {} -- reset for next time
	
	set aRes to length of fileList
	display dialog aRes as string
	
	repeat with i in fileList
		set j to POSIX path of i
		display dialog j
	end repeat
	
	tell me to quit
end doOpen

on quit
	continue quit
end quit

You may find the entire thread in http://lists.apple.com/archives/applescript-users
It was open by Takaaki Naganoya on 2017/02/17

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mardi 16 mai 2017 11:28:58

Merci Beaucoup!

I got it working. :smiley: