My question is a follow-on from this MacScripter discussion between Yvan, Shane and Nigel:
A brief recap (not least to make sure I understood it correctly):
If we try to quit and then activate an app like so
tell app "Console" to quit
tell app "Console" to activate
we end up with an error on the second command, ‘Connection is invalid’.
If we introduce a sufficient delay between the two commands, the second command should execute, but we’ll see this in the event log:
In the discussion linked to above, Shane explains (if I can put it in my simpleton’s tongue) that the error is generated because AppleScript tries to send the activate message to the target it ‘remembers’ from the first command. But, of course, that process is now dead as a result of the first command, so we get the error. Clever old AppleScript doesn’t give up though, and looks for another process, finds it or creates it, and so launches the app (in this case Console, but that’s just for the example).
Assuming I’ve more or less told the tale correctly, my questions are:
Is there any way to force AS to forget that first proc Id after the first command and before the second? Some kind of ‘flush’ I guess I mean. Why? I don’t want that error to be logged, which will only raise user questions (my users are techie enough to be sniffing around Console, even the horrible Sierra+ one).
Any suggestions on the best way to quit and immediately relaunch an app (I’ll take an answer in any language)? It is imperative that the launch does not fail. One of the reasons I’m leery about doing it this way is the dependence on the hit-and-hope delay.
This is all being done via a user domain LaunchAgent when certain conditions occur, and the agent is calling osascript (some other things happen apart from the quit/launch that make AS a better choice than a shell script, plus I need a nice dialog box, but I’ll take any other suggestions here also).