Opening an app upon quit.

Hi I am new to the forums and to applescripting in general. I was able to create an applescript that told this app (Heidrun) to run and had it repeat. So it was like this:

repeat
tell application “Heidrun” to run
end repeat

but when I quit the application the applscript said, “Heidrun got an error: Connection is invalid.”
Mostly what I am looking to do is have the same type of applescript but have it check if the application is already running or not, if possible.

Hi,

It errors like that because the process is still quitting and replies with an error I think. What you can do is use an error handler to give the process time to quit.

You can check if an app is running by checking if its process exists. This works most of the time, but sometimes it doesn’t work if the process has a different name from its app.

Also, infinite repeat loops are not good and your script should give up the cpu for a short time. You can use an idle handler or unix command ‘sleep’.

Something like this might work:

set app_name to “Heidrun”
repeat
tell application “System Events”
if (exists process app_name) then
set _exf to true
else
set _exf to false
end if
end tell
if _exf then
try
run application app_name
on error – the app is quitting
– do nothing, give it time to quit
end try
else
exit repeat
end if
do shell script “sleep 1”
end repeat
display dialog “App " & app_name & " is not running.”

gl,

Hi,

I was thinking that you may have meant that you want to run an app if it’s not running so I wrote a better script. You need to change the path to the path to the “Heidrun” app.

set app_path to “Macintosh HD:Users:kel:Desktop:Heidrun” – path to the app file
set app_name to “Heidrun”
repeat
if not GetExistsProcess(app_name) then
tell application app_path
launch
run
end tell
– wait until the process exists
repeat until GetExistsProcess(app_name)
end repeat
end if
do shell script “sleep 1” – give up cpu so you can quit this app
end repeat

on GetExistsProcess(process_name)
tell application “System Events” to exists process process_name
return result
end GetExistsProcess

gl,

Thank you so much. Now it works quite well.

Hi duffel,

You’re welcome. Glad it works.

Different apps work differently, so if you use it on certain apps, then the script may need modifications.

gl,

I found this topic while searching for a way to let a script execute if an application is running (I don’t want the script to execute the application if it isn’t running)

I tried the GetExistsProcess listed here, but it didn’t work («script» doesn’t understand the GetExistsProcess message.)

Thinking it might have something to do with the “System Information” listed in the first script, but not in the second, I updated the script to this:


tell application "System Events"
set app_name to "iTunes"
if GetExistsProcess(app_name) then
	tell application "iTunes"
		display dialog (current track's name as string)
	end tell
end if
end tell

This results in another error: System Events got an error: Can’t continue GetExistsProcess.

What’s wrong here?

When you call a suroutine from within a tell block, you need ‘my’ or ‘of me’.

my GetExistsProcess(app_name)

gl,