Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to pause Applescript when another process is not responding to system events?

I would like a Applescript to pause while a specific process/script/app is not responding (temporarily or permanently) to system events, and to start-up again when the other process is back. In theory this could be done with a repeat loop provided there is a way to monitor the status of the process. Is there any code that can do this? via Terminal commands? via Applescript? Another possibility: can Applescript determine when the cursor has switched to the beach ball? Other ideas?

Posted on Aug 9, 2015 9:57 AM

Reply
7 replies

Aug 9, 2015 10:43 AM in response to Underdog101

What specifically are you trying to do? The "not responding" state is not a process state, but rather the process has stopped communicating with the system and gets labeled as not responding when the event queue starts backing up. There isn't really an available property to check, although you might try periodically sending a with timeout statement to the application and catch any error - the script will wait until either the application responds or the statement times out (error).

Aug 9, 2015 11:59 AM in response to red_menace

The with timeout is not exactly what I'm looking for... if I'm understanding. That holds things up for a preset time. I'm trying to hold for a variable time. I could easily accomplish what I need with a repeat until or repeat while loop if only there was a way to monitor the status of the 2nd process. For example:

repeat while "beach ball is spinning"

end repeat

The status of the beach ball (on/off) is exactly what I need. You mentioned that "not responding" is not a process state. Forgive me as I'm not sure I understand exactly what that means, but even so, the information I'm looking for is obviously there - it is reported back to the GUI via the beach ball. If I can't get the "non-responding" properly directly, can I import the on/off status of the beach ball into an Applescript?

Aug 9, 2015 2:10 PM in response to Underdog101

I don't think that the beach ball is so much "reported back to the GUI" as the system (window server, event manager, whatever) just changes the cursor. Note that the busy cursor indicates that the application under it is not responding - it can still be running, it just isn't currently responding to system events.


The Activity Monitor does show an application not responding, but shell utilities such as ps don't appear to have that particular item. As far as I know there isn't anything that would be exposed to an AppleScript, so without seeing exactly what you are trying to do, periodically sending a message (with a timeout as needed) to the application and trapping an error when it doesn't respond might be the way to go.

Aug 9, 2015 2:33 PM in response to Underdog101

So how does one "periodically sending a message to the application and trapping an error when it doesn't respond". I know my way around TRY but not sending a message.

I suppose the beach ball issue is moot if there is no way to access it, but that's really too bad as it is actually uniquely informative. informative. Activity monitor tells you the app is not-responding but at that precise moment there may be several windows running under that app. If you drag the cursor over each window you find that only one is not responding.

Aug 9, 2015 3:55 PM in response to Underdog101

Sorry - when you use a tell statement you are sending a message (Apple Event) to the application for it to do something. The following snippet just tries to activate the application, but basically it repeats forever until the first tell statement succeeds:


repeat -- forever
  try
    with timeout of 10 seconds
      tell application "TextEdit" to activate -- or whatever
      exit repeat -- success
    end timeout
  on error errmess number errnum -- do whatever on timeout/error
    if errnum = -1712 then
      log "timeout error"
    else
      log errmess & space & errnum
    end if
  end try
end repeat

tell application "TextEdit" to quit -- or whatever
beep 2 -- continue

Aug 9, 2015 6:22 PM in response to Underdog101

Hello


Some years ago I've written a command line programme to check unresponsiveness of specified process id. It uses undocumented API of window server. C source code is as follows. Tested to work under 10.5.8. nda 10.6.8. Not sure about later versions.


Regards,

H



/* file main.c function check whether the application with the give process id is responding or not and print 1 if unresponsive, 0 otherwise. compile gcc -std=c99 -framework ApplicationServices -o unresponsive main.c usage e.g. ./unresponsive 354 */ #include <ApplicationServices/ApplicationServices.h> #include <libgen.h> // basename typedef int CGSConnectionID; extern bool CGSEventIsAppUnresponsive(CGSConnectionID cid, const ProcessSerialNumber *psn); extern CGSConnectionID _CGSDefaultConnection(void); int main (int argc, char * argv[]) { if ( argc != 2 ) { fprintf(stderr, "Usage: %s pid\n", basename(argv[0])); return 1; } OSStatus err; ProcessSerialNumber psn; pid_t pid = (pid_t) atoi(argv[1]); err = GetProcessForPID(pid, &psn); if ( err ) { fprintf(stderr, "Failed to get PSN for pid %d: error = %d\n", pid, err); return 2; } CGSConnectionID cid = _CGSDefaultConnection(); bool b = CGSEventIsAppUnresponsive(cid, &psn); fprintf(stdout, "%d\n", b ? 1 : 0); return 0; }

Aug 9, 2015 6:42 PM in response to Hiroto

Here's a sample code showing how to use it in AppleScript.



tell application "System Events" set pid to process "Adobe Flash Player Install Manager"'s unix id end tell pause_until_responsive(pid) display dialog "Done" on pause_until_responsive(pid) repeat until (do shell script "/usr/local/bin/unresponsive " & pid) = "0" delay 0.5 end repeat end pause_until_responsive



* The command line programme is assumed to be located at /usr/local/bin/unresponsive.



Good luck,

H

How to pause Applescript when another process is not responding to system events?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.