3

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.

1
  • Is there some reason that tell application app_name to quit doesn't work?
    – afragen
    Commented Jan 17, 2012 at 22:55

2 Answers 2

4

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
2
  • Thank sebthedev, that's what I am currently doing. However, I still don't understand why the AppleScript fail when done using the "kill" command and passing more than one PID. What's interesting is that when I run it from the terminal it works, but not the AppleScript. That's whats so annoying. Thank you for your time.
    – user16301
    Commented Jan 11, 2012 at 17:56
  • 1
    @user16301 What I've suggested to you uses the killall command, as opposed to the kill command. killall (I believe) kills apps in a different way. Have you tried using my script instead?
    – sebthedev
    Commented Jan 11, 2012 at 21:05
2

It is clear that what ever your app name is. Is also in a CrashReporter processes information.

Which I cannot replicated for obvious reasons.. (What is you app by the way)

You could just add grep -v CrashReporter

 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 | grep -v CrashReporter | 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

UPDATE:

Actually thinking about it you can use pure Applescript to get the pid of the app.

For example; if I ran your original code while looking for my app 1Password. I would get two pids. Because 1Password runs a agent 1PasswordAgent

In my script above I would invert grep (-v) to exclude anything with the word Agent.

I could if I wanted to stick with a do shell script and run.

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

Notice I add a space to the end of the app name.

But what seems the best way is running with pure applescript;

 tell application "System Events"
try
        get unix id of process "1Password"
            --> 8123
end try
    end tell

This will give me exactly what I am looking for. Because it is looking only for the "1Password".

You should use a try block on this sort of script. If the app is not running you will get an error.

2
  • This will work for most apps and should work for the OP's specific app. But, for general use, the main problem with this method, compared to using killall, is that if support processes within the .app bundle are running, grep will return those pids along with the main process. One way to get around that could be to specify part of the path to the executable inside the .app bundle rather than just the app name. E.g. "Final Cut Pro" returns 5 pids, but "MacOS/Final Cut Pro" returns only 1 pid.
    – joelseph
    Commented Feb 11, 2012 at 18:41
  • @joelseph Good points which I did realise.. But I have also updated my answer with another option.
    – markhunte
    Commented Feb 11, 2012 at 23:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .