Run in Script Editor (OK) run in Indesign Scripts (ERROR)

Hi,

I have a script that I made to run in Indesign.
When i run this script in Script Editor, its run without any errors, but if i run this same script from the Scripts tab in Indesign it skips some parts of the script making it run out of order.

Does anyone know what might be happening?


tell application "Adobe InDesign CC 2017"
	set horizontal measurement units of view preferences to millimeters
	set vertical measurement units of view preferences to millimeters
	set MyDocument1 to document 1
	set MyDocument2 to document 2
	activate
	set myDialog to make dialog
	tell myDialog
		set name to "TEST"
		set myDialogColumn to make dialog column
		tell myDialogColumn
			set myBorderPanel to make border panel
			tell myBorderPanel
				set myDialogColumn to make dialog column
				tell myDialogColumn
					make static text with properties {static label:"Ref 1:"}
				end tell
				set myDialogColumn to make dialog column
				tell myDialogColumn
					set myTextEditField1 to make text editbox with properties {edit contents:"", min width:180}
				end tell
			end tell
			set myBorderPanel to make border panel
			tell myBorderPanel
				set myDialogColumn to make dialog column
				tell myDialogColumn
					make static text with properties {static label:"Ref 2:"}
				end tell
				set myDialogColumn to make dialog column
				tell myDialogColumn
					set myTextEditField2 to make text editbox with properties {edit contents:"", min width:180}
				end tell
			end tell
			set myBorderPanel to make border panel
			tell myBorderPanel
				set myDialogColumn to make dialog column
				tell myDialogColumn
					make static text with properties {static label:"Ref 3:"}
				end tell
				set myDialogColumn to make dialog column
				tell myDialogColumn
					set myTextEditField3 to make text editbox with properties {edit contents:"", min width:180}
				end tell
			end tell
			set myBorderPanel to make border panel
			tell myBorderPanel
				set myDialogColumn to make dialog column
				tell myDialogColumn
					make static text with properties {static label:"Ref 4:"}
				end tell
				set myDialogColumn to make dialog column
				tell myDialogColumn
					set myTextEditField4 to make text editbox with properties {edit contents:"", min width:180}
				end tell
			end tell
			set myBorderPanel to make border panel
			tell myBorderPanel
				set myDialogColumn to make dialog column
				tell myDialogColumn
					make static text with properties {static label:"Ref 5:"}
				end tell
				set myDialogColumn to make dialog column
				tell myDialogColumn
					set myTextEditField5 to make text editbox with properties {edit contents:"", min width:180}
				end tell
			end tell
			set myBorderPanel to make border panel
			tell myBorderPanel
				set myDialogColumn to make dialog column
				tell myDialogColumn
					make static text with properties {static label:"Ref 6:"}
				end tell
				set myDialogColumn to make dialog column
				tell myDialogColumn
					set myTextEditField6 to make text editbox with properties {edit contents:"", min width:180}
				end tell
			end tell
		end tell
	end tell
end tell
tell application "Adobe InDesign CC 2017"
	set myResult to show myDialog
	if myResult is true then
		set myString1 to edit contents of myTextEditField1
		set myString2 to edit contents of myTextEditField2
		set myString3 to edit contents of myTextEditField3
		set myString4 to edit contents of myTextEditField4
		set myString5 to edit contents of myTextEditField5
		set myString6 to edit contents of myTextEditField6
	end if
end tell
set myRefList to {myString1, myString2, myString3, myString4, myString5, myString6}
set x1 to 10
set x2 to 10
set y1 to 60
set y2 to 60
repeat with a from 1 to count myRefList
	set ref1 to item a of myRefList
	if ref1 is not equal to "" then
		tell application "Finder"
			activate
			set parentFolder to window 1
			tell application "System Events"
				key code 3 using {command down, option down}
				delay 0.5
				keystroke ref1
				delay 0.5
				key code 48
				delay 0.5
				keystroke "a" using {command down}
			end tell
			set myList1 to selection
			if (count of myList1) is equal to 1 then
				set img1 to myList1 as string
				tell application "Adobe InDesign CC 2017"
					tell MyDocument1
						activate
						set noColor to swatch "None"
						set myRectangle to make rectangle with properties {fill color:noColor, stroke color:noColor, geometric bounds:{x1, x2, y1, y2}}
						tell myRectangle
							place alias (img1) on myRectangle
							fit myRectangle given proportionally
							fit myRectangle given center content
							fit myRectangle given frame to content
						end tell
					end tell
				end tell
			else
				repeat with b from 1 to count myList1
					set img1 to item b of myList1 as string
					tell application "Adobe InDesign CC 2017"
						tell MyDocument1
							activate
							set noColor to swatch "None"
							set myRectangle to make rectangle with properties {fill color:noColor, stroke color:noColor, geometric bounds:{x1, x2, y1, y2}}
							tell myRectangle
								place alias (img1) on myRectangle
								fit myRectangle given proportionally
								fit myRectangle given center content
								fit myRectangle given frame to content
								send to back
								set x2 to x2 + 20
								set y2 to y2 + 20
							end tell
						end tell
					end tell
				end repeat
			end if
			set x1 to x1 + 80
			set y1 to y1 + 80
			set x2 to 10
			set y2 to 60
		end tell
	end if
end repeat
set tx1 to 62
set tx2 to 10
set ty1 to 90
set ty2 to 60
set a1 to 10
set a2 to 7
set b1 to 60
set b2 to 15
repeat with c from 1 to count myRefList
	set search1 to item c of myRefList
	if search1 is not equal to "" then
		tell application "Adobe InDesign CC 2017"
			activate
			--Clear the find/change preferences.
			set find text preferences to nothing
			set change text preferences to nothing
			--Search the document for the string “”.
			set find what of find text preferences to search1
			--Set the find options.
			set case sensitive of find change text options to false
			set include footnotes of find change text options to false
			set include hidden layers of find change text options to false
			set include locked layers for find of find change text options to false
			set include locked stories for find of find change text options to false
			set include master pages of find change text options to false
			set whole word of find change text options to false
			tell application "System Events"
				key code 50 using {command down}
			end tell
			tell application "Adobe InDesign CC 2017"
				activate
				find text
				select (find text)
				tell application "System Events"
					key code 115
					delay 0.5
					key code 125 using {command down, shift down}
					key code 125 using {command down, shift down}
					key code 125 using {command down, shift down}
					delay 0.5
					keystroke "c" using {command down}
					delay 0.5
					key code 50 using {command down, shift down}
				end tell
				tell MyDocument1
					activate
					set myPaste1 to the clipboard
					set myTextFrame1 to make text frame with properties {geometric bounds:{tx1, tx2, ty1, ty2}}
					set contents of myTextFrame1 to myPaste1
					select every text of myTextFrame1
					delay 1
					tell application "System Events"
						key code 84 using {shift down}
						delay 0.2
						tell application "Adobe InDesign CC 2017" to select first line of myTextFrame1
						delay 0.2
						key code 83 using {shift down}
						delay 0.2
						tell application "Adobe InDesign CC 2017" to select last line of myTextFrame1
						delay 0.2
						key code 85 using {shift down}
					end tell
					set old_delimts to AppleScript's text item delimiters
					delete last character of last line of myTextFrame1
					select last line of myTextFrame1
					set myColors1 to selection as string
					fit myTextFrame1 given frame to content
					set AppleScript's text item delimiters to " "
					set myColorList1 to every text item of myColors1
					if (count of myColorList1) > 1 then
						repeat with c1 from 1 to count myColorList1
							set color1 to item c1 of myColorList1
							set myColor1 to make text frame with properties {geometric bounds:{a2, a1, b2, b1}}
							set contents of myColor1 to color1
							select every text of myColor1
							tell application "System Events" to key code 85 using {shift down}
							delay 0.2
							fit myColor1 given frame to content
							set a1 to a1 + 20
							set b1 to b1 + 20
						end repeat
					else
						set myColor1 to make text frame with properties {geometric bounds:{a2, a1, b2, b1}}
						set contents of myColor1 to myColors1
						select every text of myColor1
						delay 1
						tell application "System Events" to key code 85 using {shift down}
						fit myColor1 given frame to content
						set AppleScript's text item delimiters to old_delimts
					end if
					repeat 2 times
						select last line of myTextFrame1
						delete selection
					end repeat
					delete last character of last line of myTextFrame1
					fit myTextFrame1 given frame to content
				end tell
			end tell
			set tx1 to tx1 + 80
			set ty1 to ty1 + 80
			set tx2 to 10
			set ty2 to 60
			set a2 to a2 + 80
			set b2 to b2 + 80
			set a1 to 10
			set b1 to 60
		end tell
	end if
end repeat

Hi there,

The code below is untested and more of an illustration.

I’ve put everything in one ‘tell InDesign’ block to hopefully simplify things.

Can a lot of what you’re doing with System Events key codes be replaced with actual commands in the Finder or InDesign?

You could try ‘commenting out’ sections of code to try and ascertain where the problems start.

HTH


tell application "Adobe InDesign CC 2017"
	
	set horizontal measurement units of view preferences to millimeters
	set vertical measurement units of view preferences to millimeters
	
	set MyDocument1 to document 1
	set MyDocument2 to document 2
	
	activate
	
	set myDialog to make dialog
	tell myDialog
		set name to "TEST"
		set myDialogColumn to make dialog column
		tell myDialogColumn
			set myBorderPanel to make border panel
			tell myBorderPanel
				set myDialogColumn to make dialog column
				tell myDialogColumn
					make static text with properties {static label:"Ref 1:"}
				end tell
				set myDialogColumn to make dialog column
				tell myDialogColumn
					set myTextEditField1 to make text editbox with properties {edit contents:"", min width:180}
				end tell
			end tell
			set myBorderPanel to make border panel
			tell myBorderPanel
				set myDialogColumn to make dialog column
				tell myDialogColumn
					make static text with properties {static label:"Ref 2:"}
				end tell
				set myDialogColumn to make dialog column
				tell myDialogColumn
					set myTextEditField2 to make text editbox with properties {edit contents:"", min width:180}
				end tell
			end tell
			set myBorderPanel to make border panel
			tell myBorderPanel
				set myDialogColumn to make dialog column
				tell myDialogColumn
					make static text with properties {static label:"Ref 3:"}
				end tell
				set myDialogColumn to make dialog column
				tell myDialogColumn
					set myTextEditField3 to make text editbox with properties {edit contents:"", min width:180}
				end tell
			end tell
			set myBorderPanel to make border panel
			tell myBorderPanel
				set myDialogColumn to make dialog column
				tell myDialogColumn
					make static text with properties {static label:"Ref 4:"}
				end tell
				set myDialogColumn to make dialog column
				tell myDialogColumn
					set myTextEditField4 to make text editbox with properties {edit contents:"", min width:180}
				end tell
			end tell
			set myBorderPanel to make border panel
			tell myBorderPanel
				set myDialogColumn to make dialog column
				tell myDialogColumn
					make static text with properties {static label:"Ref 5:"}
				end tell
				set myDialogColumn to make dialog column
				tell myDialogColumn
					set myTextEditField5 to make text editbox with properties {edit contents:"", min width:180}
				end tell
			end tell
			set myBorderPanel to make border panel
			tell myBorderPanel
				set myDialogColumn to make dialog column
				tell myDialogColumn
					make static text with properties {static label:"Ref 6:"}
				end tell
				set myDialogColumn to make dialog column
				tell myDialogColumn
					set myTextEditField6 to make text editbox with properties {edit contents:"", min width:180}
				end tell
			end tell
		end tell
		
	end tell
	
	set myResult to show myDialog
	
	if myResult is true then
		set myString1 to edit contents of myTextEditField1
		set myString2 to edit contents of myTextEditField2
		set myString3 to edit contents of myTextEditField3
		set myString4 to edit contents of myTextEditField4
		set myString5 to edit contents of myTextEditField5
		set myString6 to edit contents of myTextEditField6
	end if
	
	set myRefList to {myString1, myString2, myString3, myString4, myString5, myString6}
	set x1 to 10
	set x2 to 10
	set y1 to 60
	set y2 to 60
	
	repeat with a from 1 to count myRefList
		
		set ref1 to item a of myRefList
		
		if ref1 is not equal to "" then
			
			tell application "Finder"
				activate
				set parentFolder to window 1
				
				tell application "System Events"
					key code 3 using {command down, option down}
					delay 0.5
					keystroke ref1
					delay 0.5
					key code 48
					delay 0.5
					keystroke "a" using {command down}
				end tell
				
				set myList1 to selection
			end tell
			
			if (count of myList1) is equal to 1 then
				set img1 to myList1 as string
				
				tell MyDocument1
					
					activate
					
					set noColor to swatch "None"
					set myRectangle to make rectangle with properties {fill color:noColor, stroke color:noColor, geometric bounds:{x1, x2, y1, y2}}
					
					tell myRectangle
						place alias (img1) on myRectangle
						fit myRectangle given proportionally
						fit myRectangle given center content
						fit myRectangle given frame to content
					end tell
					
				end tell
				
			else
				repeat with b from 1 to count myList1
					
					set img1 to item b of myList1 as string
					
					tell MyDocument1
						
						activate
						
						set noColor to swatch "None"
						set myRectangle to make rectangle with properties {fill color:noColor, stroke color:noColor, geometric bounds:{x1, x2, y1, y2}}
						
						tell myRectangle
							place alias (img1) on myRectangle
							fit myRectangle given proportionally
							fit myRectangle given center content
							fit myRectangle given frame to content
							send to back
							set x2 to x2 + 20
							set y2 to y2 + 20
						end tell
						
					end tell
					
				end repeat
			end if
			
			set x1 to x1 + 80
			set y1 to y1 + 80
			set x2 to 10
			set y2 to 60
			
		end if
	end repeat
	
	
	set tx1 to 62
	set tx2 to 10
	set ty1 to 90
	set ty2 to 60
	set a1 to 10
	set a2 to 7
	set b1 to 60
	set b2 to 15
	
	
	repeat with c from 1 to count myRefList
		
		set search1 to item c of myRefList
		
		if search1 is not equal to "" then
			--Clear the find/change preferences.
			set find text preferences to nothing
			set change text preferences to nothing
			--Search the document for the string “”.
			set find what of find text preferences to search1
			--Set the find options.
			set case sensitive of find change text options to false
			set include footnotes of find change text options to false
			set include hidden layers of find change text options to false
			set include locked layers for find of find change text options to false
			set include locked stories for find of find change text options to false
			set include master pages of find change text options to false
			set whole word of find change text options to false
			
			tell application "System Events"
				key code 50 using {command down}
			end tell
			
			find text
			select (find text)
			
			tell application "System Events"
				key code 115
				delay 0.5
				key code 125 using {command down, shift down}
				key code 125 using {command down, shift down}
				key code 125 using {command down, shift down}
				delay 0.5
				keystroke "c" using {command down}
				delay 0.5
				key code 50 using {command down, shift down}
			end tell
			
			tell MyDocument1
				
				activate
				
				set myPaste1 to the clipboard
				set myTextFrame1 to make text frame with properties {geometric bounds:{tx1, tx2, ty1, ty2}}
				set contents of myTextFrame1 to myPaste1
				select every text of myTextFrame1
				
				delay 1
				
				tell application "System Events"
					key code 84 using {shift down}
					delay 0.2
				end tell
				
				select first line of myTextFrame1
				
				tell application "System Events"
					delay 0.2
					key code 83 using {shift down}
					delay 0.2
				end tell
				
				select last line of myTextFrame1
				
				tell application "System Events"
					delay 0.2
					key code 85 using {shift down}
				end tell
				
				set old_delimts to AppleScript's text item delimiters
				delete last character of last line of myTextFrame1
				select last line of myTextFrame1
				set myColors1 to selection as string
				fit myTextFrame1 given frame to content
				set AppleScript's text item delimiters to " "
				set myColorList1 to every text item of myColors1
				
				if (count of myColorList1) > 1 then
					
					repeat with c1 from 1 to count myColorList1
						set color1 to item c1 of myColorList1
						set myColor1 to make text frame with properties {geometric bounds:{a2, a1, b2, b1}}
						set contents of myColor1 to color1
						select every text of myColor1
						
						tell application "System Events" to key code 85 using {shift down}
						
						delay 0.2
						fit myColor1 given frame to content
						set a1 to a1 + 20
						set b1 to b1 + 20
					end repeat
					
				else
				
					set myColor1 to make text frame with properties {geometric bounds:{a2, a1, b2, b1}}
					set contents of myColor1 to myColors1
					select every text of myColor1
					delay 1
					tell application "System Events" to key code 85 using {shift down}
					fit myColor1 given frame to content
					set AppleScript's text item delimiters to old_delimts
					
				end if
				
				repeat 2 times
					select last line of myTextFrame1
					delete selection
				end repeat
				
				delete last character of last line of myTextFrame1
				fit myTextFrame1 given frame to content
				
			end tell
			
			
			set tx1 to tx1 + 80
			set ty1 to ty1 + 80
			set tx2 to 10
			set ty2 to 60
			set a2 to a2 + 80
			set b2 to b2 + 80
			set a1 to 10
			set b1 to 60
			
		end if
		
	end repeat
	
end tell


Try to save the script as an application and then run it form the scripts pane, I have had success with this in the past!