MacOS – Process not running but app shown in dock

applescriptdockmacos

I have an applescript that runs automatically and relies on the application "Contacts" to be running. Occasionally, it fails when the "Contacts" process mysteriously disappears from the activity monitor, although the dock still shows it as running.

The following will NOT work:

  1. Applescript "tell application "Contacts" to activate
  2. Applescript "tell application "Contacts' to quit
    (both respond with "application not running)
  3. Applescript "do shell script killall launchserviced" followed by "killall dock" executes ok but doesn't solve the problem

Only solution is to MANUALLY quit "Contacts" from the dock and then reactivate it but that solution is not satisfactory because it requires a manual intervention.
Any ideas as to a) why the error happens and b) how can I cater for the error by quiting "Contacts" from applescript ?

Any ideas will be appreciated

….

In response to bmike's comment;

Running OS X 10.10.5 and here is the relevant section of the applescript

set todaysdate to (current date) as text

*tell application "System Events"

try
    set myprocessid to (get id of process "Contacts")   
on error number errorno
    display dialog errorno
    if errorno is equal to -1728 then -- process not found
        try
            tell application "System Events" to run application "Contacts"
        on error number errorno
            if errorno = -600 then
                display dialog errorno & todaysdate as text
            end if
        end try
        do shell script "/bin/sleep 3" --and allow enough time for the app to activate
    end if
end try

end tell

This section attempts to activate "Contacts" if it finds the process is not running (error -1728). It then attempts to run the app but it always errors out with -600 (app not running). A simple applescript to quit the app also errors out with -600.

So, if the process is missing, the app cannot be activated and quit does not work either.

Thanks for your comments

Best Answer

I seemed to have at last found a way to get over this. It's rather crude, but worked for me.

if errorno = -600 then tell application "System Events" tell process "Dock" click UI element "Contacts" of list 1 end tell end tell delay 10 -- allow sometime for the process to appear in the process list tell application "System Events" tell process "Contacts" to set visible to false -- run hidden end tell end if

By forcing a click in the dock, the process re-appears in the process list and then the app can be quit, or restarted, or whatever. Weird.