Using Keystroke I'm trying to fill a UI form, which I'm find issue

Hi All,

I’m using “Keystroke” to fill the UI with the assigned variable, but when I switch to another application the script continues to apply the keystroke.
to control this I used the below function:
on ActiveError()

global frontApp, frontAppName, windowTitle

set windowTitle to ""
tell application "System Events"
	set frontApp to first application process whose frontmost is true
	set frontAppName to name of frontApp
	tell process frontAppName
		tell (1st window whose value of attribute "AXMain" is true)
			set windowTitle to value of attribute "AXTitle"
		end tell
	end tell
end tell

if frontAppName is not equal to "Adobe InDesign 2022" then
	
	tell application "Adobe InDesign 2022" to activate
	
	-CustomAlert()
	
end if

return {frontAppName, windowTitle}

end ActiveError

My Question is:

When I use the above code script takes too long to perform, I need to add this before for each and every line to control the cmd+tab.

Are there any other alternate solutions?

Thank you

@Ashwin_Gopal - Why are you trying to use keystrokes to script InDesign? It’s far more reliable to script InDesign directly. Most things can be done directly in InDesign with the InDesign dictionary.