Need to figure out when one process is done before starting another

We’re moving to InDesign and have the Markzware Q2ID plugin. I wrote this script that uses UI scripting to convert the quark files using Markzware and then it tells InDesign to do a few things with each file before saving. My only problem is that InDesign is trying to process the converted files BEFORE the Markzware plugin is done converting. I used a long delay to prevent that, but the time it takes to convert the files will vary depending on the number of files selected.

Is there a better way to get InDesign to wait for the plugin to finish before the script moves on to processing the open documents?



-----------------------------------------------------------------------------------------		

--DISPLAYING A MESSAGE TO THE USER
tell application "SystemUIServer"
	display dialog "This script is going to take a little while. You can move on to something else. It will display a message when done." with icon note
end tell

-----------------------------------------------------------------------------------------		



-----------------------------------------------------------------------------------------		

--CHOOSING FILES TO CONVERT
my do_submenu("Adobe InDesign CC 2014", "Markzware", "Q2ID", "Convert QuarkXPress(c) Document...")

-----------------------------------------------------------------------------------------		



-----------------------------------------------------------------------------------------	

--DELAYING THE SCRIPT 10 MINUTES TO GIVE THE CONVERTER TIME BEFORE THE INDESIGN PART RUNS
delay 600 -- delaying 10 minutes before processing files

-----------------------------------------------------------------------------------------		



-----------------------------------------------------------------------------------------		

--GETTING RID OF ANNOYING POP UP BOXES IN INDESIGN
tell application "Adobe InDesign CC 2014"
	activate
	
	
	set user interaction level of script preferences to never interact -->Prevents InDesign from popping up warnings that interrupt the script.
	
	tell view preferences
		set horizontal measurement units to inches
		set vertical measurement units to inches
	end tell
	
	-----------------------------------------------------------------------------------------		
	
	
	
	-----------------------------------------------------------------------------------------		
	
	--FITTING TEXT FRAMES TO THE CONTENT IF THERE IS AN OVERFLOW
	--THIS WAY WE DON'T RISK HAVING CUT OFF TEXT IN AN AD
	
	
	set allFiles to every document
	repeat with afile in allFiles
		
		tell document 1
			set locked of every page item to false
			
			--Looking through every text frame and fitting the frame to content if there is overset text
			set everyTextFrame to every text frame
			repeat with textFrame in everyTextFrame
				if overflows of textFrame then
					fit textFrame given frame to content
					--set textFrameContents to stories of textFrame
					--set horizontal scale of textFrameContents to "105"
					--set horizontal scale of textFrameContents to "100"
				end if
			end repeat
			
			-----------------------------------------------------------------------------------------		
			
			
			
			-----------------------------------------------------------------------------------------	
			
			--SELECTIVELY MOVING TEXT BOXES TO THE FRONT
			--BECAUSE THE TEXT HAS LINES THROUGH IT IF IS NOT BROUGHT TO THE FRONT
			
			--Bringing each text frame to the front IF the fill color of that text frame is "None"
			--It could mess up the layout if text boxes with a background color were brought to the front, so I added a condition to make sure that doesn't happen.
			set theTextFrames to (every text frame whose name of fill color is "None")
			bring to front theTextFrames
		end tell
		
		-----------------------------------------------------------------------------------------	
		
		
		
		-----------------------------------------------------------------------------------------		
		
		--REMOVING _LAYOUT 1 FROM THE FILENAME
		
		set oldDelims to AppleScript's text item delimiters
		
		--ELIMINATING _LAYOUT 1 IN FILENAME
		if name of afile contains "_Layout 1" then
			set AppleScript's text item delimiters to {"_Layout 1"}
			
			set fileName to name of afile --> ex. Disneyland.tif
			set nameWithoutExtension to first text item of fileName --> Disneyland
			set newName to nameWithoutExtension & ".indd" --> Disneyland_s_4c.tif
			
		else
			set AppleScript's text item delimiters to {""}
		end if
		
		
		--RESETTING APPLESCRIPT'S TEXT ITEM DELIMITERS
		set AppleScript's text item delimiters to oldDelims
		
		-----------------------------------------------------------------------------------------		
		
		
		
		-----------------------------------------------------------------------------------------		
		
		--SAVING AND CLOSING THE DOCUMENT ON THE DESKTOP
		set saveFolder to path to desktop as string
		set savePath to saveFolder & newName
		save active document to savePath
		close active document
	end repeat
end tell

-----------------------------------------------------------------------------------------		




-----------------------------------------------------------------------------------------		

--DISPLAYING A MESSAGE TO LET THE USER KNOW THIS IS DONE
tell application "SystemUIServer"
	display dialog "The pages are converted and saved on your desktop!" with icon note
end tell

-----------------------------------------------------------------------------------------	





--------------------------------------------


--The UI Scripting select submenu function
--Thanks to http://www.macosxautomation.com/applescript/uiscripting/
on do_submenu(app_name, menu_name, menu_item, submenu_item)
	try
		-- bring the target application to the front
		tell application app_name
			activate
		end tell
		tell application "System Events"
			tell process app_name
				tell menu bar 1
					pick menu bar item menu_name
					tell menu bar item menu_name
						tell menu menu_name
							pick menu item menu_item
							tell menu item menu_item
								tell menu menu_item
									pick menu item submenu_item
									click menu item submenu_item
								end tell
							end tell
						end tell
					end tell
				end tell
			end tell
		end tell
		return true
	on error error_message
		return false
	end try
end do_submenu

AppleScript: AppleScript 2.2.1
Browser: Safari 537.78.2
Operating System: Mac OS X (10.7)

Here’s what I did. Towards the beginning of the script, I prompt the user for the number of pages that are going to be dropped on the script. I then do a calculation that multiplies the number they enter by 30. Then, I set a delay for the amount of time figured in that calculation. It’s not the most elegant solution, but it does prevent having too short of a delay or an insanely long one.

-----------------------------------------------------------------------------------------		

--WHAT THIS SCRIPT DOES:
--It creates a folder on the user's desktop named "Converted InDesign Files" if that folder doesn't exist yet
--It sets a bunch of text preferences to attempt to make the text not look smushed upon conversion
--It prompts the user for the number of pages that will be dropped on the script 
--(The InDesign part needs to be delayed about 30 seconds for every page that has to convert.)
--It clicks on the ConvertQuarkXPress© Document. in the menu, assuming the user has the Markzware plugin installed
--It delays the script so that the pages have time to finish converting before moving on to the InDesign part
--It suppresses all interrupting popup/dialog boxes
--If a text frame is overflowing, it fits that text frame to the text, eliminating all overflows
--It brings all text frames with a transparent background to the front
--It removes _Layout 1 from the filename, since InDesign feels compelled to add that to the filename

-----------------------------------------------------------------------------------------		




-----------------------------------------------------------------------------------------		

--CREATING CONVERTED FOLDER IF IT DOESN'T EXIST YET
set desktopFolder to path to desktop as string
if not my CheckForFolder(desktopFolder & "Converted InDesign Files:") then
	tell application "Finder"
		make new folder at folder desktopFolder with properties {name:"Converted InDesign Files"}
	end tell
end if

set convertedFolder to desktopFolder & "Converted InDesign Files" & ":" as string

-----------------------------------------------------------------------------------------		




-----------------------------------------------------------------------------------------		

--SETTING TEXT PREFERENCES IN AN ATTEMPT TO KEEP THE TEXT FROM IMPORTING ALL SMUSHED

tell application "Adobe InDesign CC 2014"
	tell text import preferences
		
		--The dictionary property can take many values, such as French, Italian.
		set dictionary to "English: USA"
		--platform options:
		--macintosh
		--pc
		set platform to macintosh
		set strip returns between lines to true
		set strip returns between paragraphs to true
		set use typographers quotes to true
	end tell
	
	tell text preferences
		set z order text wrap to true
		set use paragraph leading to true
	end tell
	
	tell text defaults
		set auto leading to 100
		set baseline shift to 0
		set composer to "Adobe Paragraph Composer"
		set desired letter spacing to 0
		set desired word spacing to 105
		set horizontal scale to 105
		set vertical scale to 100
		set keep all lines together to false
		set keep lines together to false
		set kerning method to "Optical"
		set tracking to 0
		set no break to false
		set keep first lines to 1
	end tell
	
	--Resetting a couple of text defaults because changing the scale/tracking
	--seems to reflow the text in Indesign
	tell text defaults
		set tracking to 1
		set horizontal scale to 100
	end tell
	
end tell

-----------------------------------------------------------------------------------------		



-----------------------------------------------------------------------------------------		

--PROMPTING THE USER FOR THE NUMBER OF PAGES/FILES AND CALCULATING THE DELAY TIME
tell application "SystemUIServer"
	set pagePrompt to display dialog "How many pages are you dropping on this script?" default answer "Example: 24" buttons {"Ok"} with icon note --The user prompt for the number of pages
	set pageCount to text returned of pagePrompt as number
	if pageCount does not contain "Example" then
		set delayTime to pageCount * 30 --Calculating the number of seconds to delay the script before moving on to the InDesign part (It takes about 30 seconds to convert each page/file)
		
		--Prompting the user again if they just pressed enter and forgot to type in a number
	else if pageCount contains "Example" then
		set pagePrompt2 to display dialog "Did you remember to enter the number of pages you are dropping on this script? Please enter number of pages below:" default answer "Example: 24" buttons {"Ok"} with icon note --The user prompt for the number of pages
		set pageCount2 to text returned of pagePrompt2
		set delayTime to pageCount2 * 30
	end if
	
end tell

-----------------------------------------------------------------------------------------		



-----------------------------------------------------------------------------------------		

--CHOOSING FILES TO CONVERT
my do_submenu("Adobe InDesign CC 2014", "Markzware", "Q2ID", "Convert QuarkXPress(c) Document...")

-----------------------------------------------------------------------------------------		



-----------------------------------------------------------------------------------------	

--DELAYING THE SCRIPT 5 MINUTES TO GIVE THE CONVERTER TIME BEFORE THE INDESIGN PART RUNS
delay delayTime -- delaying 30 seconds per page before processing files

-----------------------------------------------------------------------------------------		



-----------------------------------------------------------------------------------------		

--GETTING RID OF ANNOYING POP UP BOXES IN INDESIGN
tell application "Adobe InDesign CC 2014"
	activate
	
	
	set user interaction level of script preferences to never interact -->Prevents InDesign from popping up warnings that interrupt the script.
	
	tell view preferences
		set horizontal measurement units to inches
		set vertical measurement units to inches
	end tell
	
	-----------------------------------------------------------------------------------------		
	
	
	
	-----------------------------------------------------------------------------------------		
	
	--WORKING ON THIS PART
	--CHANGING TAB POSITION OF PAGE NUMBERS AT BOTTOM SO IT'S LINED UP CORRECTLY
	set allFiles to every document
	repeat with afile in allFiles
		
		--Unlocking everything just in case something was locked
		tell document 1
			set locked of every page item to false
			
			--Creating a list of possible page numbers, to be used later in searching for characters within the text frame
			set possiblePageNumbers to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99"}
			
			
			--Getting the page height
			tell page 1
				set pageBounds to get bounds
				set pageHeight to item 3 of bounds
			end tell
			
			--Looping through the text in every text frame
			set everyTextFrame to every text frame
			repeat with textFrame in everyTextFrame
				set textOfTextFrame to characters of textFrame
				
				--Determining if bottom a text frame is less than 1" above the bottom of the page
				--since the page numbers will be at the bottom of the page
				tell textFrame
					set textFrameBounds to geometric bounds
					set y3 to item 3 of textFrameBounds
					set tabCutoff to (pageHeight - 0.75)
					
					--Determining if the characters in the text frame are in the above possiblePageNumbers list
					if textOfTextFrame is in possiblePageNumbers then
						set pageNumber to true
					else
						set pageNumber to false
					end if
					
					--WORKING ON THIS PART
					--Change the tabs in the text frame if the bottom of the frame is less than 0.75" above the bottom of the page 
					--and the text frame contains a number from the above possiblePageNumbers list
					if y3 is less than tabCutoff and pageNumber is true then
						--commands to set tab position will go here
					end if
				end tell
				
				-----------------------------------------------------------------------------------------		
				
				
				
				-----------------------------------------------------------------------------------------		
				
				--FITTING TEXT FRAMES TO THE CONTENT IF THERE IS AN OVERFLOW
				--THIS WAY WE DON'T RISK HAVING CUT OFF TEXT IN AN AD
				
				if overflows of textFrame then
					fit textFrame given frame to content
					
					--Looking through every anchored text frame within each text frame 
					--fitting the anchored text frame to content if there is overset text
					set anchoredTextFrames to all page items of textFrame
					repeat with i from 1 to number of anchoredTextFrames
						set anchoredTextFrame to item i of anchoredTextFrames
						if overflows of anchoredTextFrame then
							fit anchoredTextFrame given frame to content
						end if
					end repeat
					
				end if
			end repeat
			
			
			-----------------------------------------------------------------------------------------		
			
			
			
			-----------------------------------------------------------------------------------------	
			
			--SELECTIVELY MOVING TEXT BOXES TO THE FRONT
			--BECAUSE THE TEXT HAS LINES THROUGH IT IF IS NOT BROUGHT TO THE FRONT
			
			--Bringing each text frame to the front IF the fill color of that text frame is "None"
			--It could mess up the layout if text boxes with a background color were brought to the front, so I added a condition to make sure that doesn't happen.
			set theTextFrames to (every text frame whose name of fill color is "None")
			bring to front theTextFrames
		end tell
		
		-----------------------------------------------------------------------------------------	
		
		
		
		-----------------------------------------------------------------------------------------		
		
		--REMOVING _LAYOUT 1 FROM THE FILENAME
		
		set oldDelims to AppleScript's text item delimiters
		
		--ELIMINATING _LAYOUT 1 IN FILENAME
		if name of afile contains "_Layout 1" then
			set AppleScript's text item delimiters to {"_Layout 1"}
			
			set fileName to name of afile --> ex. Disneyland.tif
			set nameWithoutExtension to first text item of fileName --> Disneyland
			set newName to nameWithoutExtension & ".indd" --> Disneyland_s_4c.tif
			
		else
			set AppleScript's text item delimiters to {""}
		end if
		
		
		--RESETTING APPLESCRIPT'S TEXT ITEM DELIMITERS
		set AppleScript's text item delimiters to oldDelims
		
		-----------------------------------------------------------------------------------------		
		
		
		
		-----------------------------------------------------------------------------------------		
		
		--SAVING AND CLOSING THE DOCUMENT ON THE DESKTOP
		
		set savePath to convertedFolder & newName
		save active document to savePath
		close active document
	end repeat
end tell

-----------------------------------------------------------------------------------------		



-----------------------------------------------------------------------------------------		

--DISPLAYING A MESSAGE TO LET THE USER KNOW THIS IS DONE
tell application "SystemUIServer"
	display dialog "The pages are converted and saved on your desktop!" with icon note
end tell

-----------------------------------------------------------------------------------------	



--------------------------------------------

--The UI Scripting select submenu function
--Thanks to http://www.macosxautomation.com/applescript/uiscripting/
on do_submenu(app_name, menu_name, menu_item, submenu_item)
	try
		-- bring the target application to the front
		tell application app_name
			activate
		end tell
		tell application "System Events"
			tell process app_name
				tell menu bar 1
					pick menu bar item menu_name
					tell menu bar item menu_name
						tell menu menu_name
							pick menu item menu_item
							tell menu item menu_item
								tell menu menu_item
									pick menu item submenu_item
									click menu item submenu_item
								end tell
							end tell
						end tell
					end tell
				end tell
			end tell
		end tell
		return true
	on error error_message
		return false
	end try
end do_submenu



--FUNCTION TO CHECK IF A FOLDER EXISTS
on CheckForFolder(thisFolder)
	tell application "Finder"
		return (exists folder thisFolder)
	end tell
end CheckForFolder