If the app is active

I have created a Script and I have exported it as an app. I have selected “Stay open after run handler” But I want the script to run only when the app is selected (active) and stop when it is not.

Something like:
if myApp is active then
do something
else
stop
end if

How can I do that?

(I want to detect when myApp, is active. I mean the app that I have created with the script)

Thank you but when I say “myApp” I mean the app that I have created with the script.

Ok. Great. It works.

When it is not active the app is bouncing on the doc all the time. Do you know how to avoid that?

You want execute some code (-- do something) only when your stay-open app is frontmost? Like here:


property myAppName : "Untitled"

on idle
	tell application "System Events" to set isFrontmostApp to frontmost of process myAppName
	if isFrontmostApp then
		-- do something
		display notification "TEST dialog"
	else
		--do nothing
	end if
	return 5
end idle

Yes! thank you.

Just to learn. In a previous line you use “giving up after 3” What does it do? (it gives more than 3 dialogs)

It is test dialogs which gave up after 3 seconds automatically (if the user during 3 seconds doesn’t press any its button, it disappears itself after 3 seconds). You should replace them with your real code. I will repeat code here for those users, who are interested:


property myAppName : "Untitled"

on idle
	tell application "System Events" to set isFrontmostApp to frontmost of process myAppName
	if isFrontmostApp then
		-- do something
		display dialog "TEST dialog" giving up after 3
	else
		--do nothing
	end if
	return 5
end idle

Now, I still can’t understand what you want to achieve. So, I provide here code sceleton in case you want display dailogs of your stay-open app to be always on front (app is bouncing on the doc all the time when some dialog window of it is on the background, because dialod waits for response from user, but user doesn’t see the dialog)


on idle
	activate me -- THIS
	display dialog "TEST dialog"
	return 5
end idle