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:
tell application “Heidrun” to run
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.
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”
tell application “System Events”
if (exists process app_name) then
set _exf to true
set _exf to false
if _exf then
run application app_name
on error – the app is quitting
– do nothing, give it time to quit
do shell script “sleep 1”
display dialog “App " & app_name & " is not running.”
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”
if not GetExistsProcess(app_name) then
tell application app_path
– wait until the process exists
repeat until GetExistsProcess(app_name)
do shell script “sleep 1” – give up cpu so you can quit this app
tell application “System Events” to exists process process_name
Thank you so much. Now it works quite well.
You’re welcome. Glad it works.
Different apps work differently, so if you use it on certain apps, then the script may need modifications.
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)
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’.