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

Most of the time this works, but in your case if InDesign is not started you need to
use application id… to launch it.

tell application id "com.adobe.InDesign" to launch

To make InDesign to become frontmost you use set frontmost to true

tell application "System Events"
	tell application process <YOUR_APP_NAME>
		set frontmost to true
		-- some code
	end tell
end tell