AppleScript “Kill” Command Error

applescript

I am attempting to run an AppleScript every 24 hours to close a running application and restart it. I have used the following code successfully with other Apps:

 repeat

 delay 84600

 set app_name to "APPLICATION NAME"

 set the_pid to (do shell script "ps ax | grep " & (quoted form of app_name) & " | grep -v grep | awk '{print $1}'")

 if the_pid is not "" then do shell script ("kill -9 " & the_pid)

 delay 30

 tell application app_name to activate

 end repeat

However, when I run this script I get the error that Kill: argument must be process or job id.

When I check the value of the_pid, it returns with the correct PID, however, it returns more than one PID. For example, it return 5430 and 5960, one which corresponds to the application and the other to CrashReporter. Hence, I believe that kill command fails due to more than one PID being present in the the_pid variable.

Does anyone know how to get around/fix this? How to make kill accept the the_pid variable when it contains more than one PID? Is it a matter of formating?

I know I can always use killall and pass the name of the application but there is no technical reason why this should not work.

Best Answer

You can do what you are trying to do using the killall command and referring directly to the application's name.

repeat
 delay 84600
 set app_name to "APPLICATION NAME"
 do shell script "killall " & app_name
 delay 30
 tell application app_name to activate
end repeat