Hang on Quitting Apple Messages

Part of a script I have written uses a list to close applications that are running.

It works perfectly EXCEPT, now and then, for the application ‘Messages’ it hangs a long time, quits Messages, and then continues properly.

What is so unique about Messages? Is there any explanation?

repeat with i from 1 to NLines

   tell application (item i of CAList) to if it is running then quit

   repeat 5 times
      if application (item i of CAList) is not running then exit repeat
      delay 0.5
   end repeat
   
end repeat

I don’t know why the application hung up. Ask to Apple.

But we can detect the status of application process via shell commands.

And…If the application hung up, AppleScript command will be not accepted.
So, you have to check with frequently crash prone application (ex: Adobe InDesign) via unix shell or Cocoa methods.

AppleScript way is…using “with timeout of xxx” phrase with very short time.

try
with timeout of 3 seconds
	--write here some script to control target application
end timeout
on error
	--error handling here
end try

.
You don’t have a very good workround for checking and exiting applications. For example, even if the application is not running, then the repeat loop is still executed, which in this case should not be.

I rewrote your script in other form, which quits the Message.app without problems:
.

set CAList to {"Transmission", "MediaInfo", "Messages"}

repeat with i from 1 to (count CAList)
	tell application (item i of CAList)
		
		if it is running then
			quit it
			repeat while it is running
				delay 0.2
			end repeat
		end if
		
	end tell
end repeat

Why is it necessary to repeat the quit it in a repeat loop? I’d expect the app to simply die when it’s called to quit. But perhaps it’s not…?

I don’t know why the OP needs this, but this check makes the script stable when the application needs to be restarted. Often if you try to activate the application right after the quit command, the script will throw an error. Also, this check can be used to get stability in GUI scripting.
.
There are many examples of restarting System Preferences.app (System Settings.app in new OS) where this approach provides stability.
.

tell application "TextEdit"
	activate
	quit
	activate
end tell

--> error: "TextEdit got an error: Connection is invalid."

.
Here all go OK:
.

tell application "TextEdit"
	activate
	quit
	repeat while it is running
		delay 0.2
	end repeat
	activate
end tell

--> OK
1 Like

Odd that Messages is hanging…

I’d try something like this:

set quitAppList to {"Calculator", "Messages", "TextEdit"}

repeat with appName in quitAppList
   tell application appName
      if it is running and appName is "Messages" then
         ignoring application responses
            quit it
         end ignoring
      else if it is running then
         quit it
      end if
   end tell
end repeat

For my own use I’d probably skip the extra IF and just use the ignoring specifier for all the apps.

Currently running macOS 10.14.6 Mojave and Apple Messages 12.0.

It’s not really clear if anyone here has had the same bad experience with Messages.app as the OP.

Also, it is desirable not to complicate the test, at least not yet. For example, after running the following script on my Mac, the Messages.app does not just exit, but “crashes with a whistle”.

(macOS Catalina)

tell application "Messages" to quit

If only the OP is experiencing the problem, then this indicates that the problem is local, on their computer.

I understand your script, I think. But if one of the applications in CAList hangs, there is no exit for the repeat. It may have quit Messages without problems for you, but so does mine most of the time. Once in many runs mine takes a much longer time - sometimes a full minute or two - to quit. So I cannot be sure yours is a solution and, as I said, the possibility of a non-terminating loop is a problem.

I am inclined to try your approach but I will not know right away if it solves the problem because my script only hangs a bit very infrequently. I will post back immediately if I still have this problem with your script or else I will post back much later if I think it never fails.
By the way, what does ignoring application responses ignore? - ‘Ask to save changes’? - what else?