Quit & Restart same app, forgetting previous proc

asobjc

(Phil Stokes) #1

My question is a follow-on from this MacScripter discussion between Yvan, Shane and Nigel:

http://macscripter.net/viewtopic.php?id=45151

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:

  1. 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).

  2. 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).


(Jonas Whale) #2

Try this:

try
	tell application "Console" to quit
	delay 1
end try

tell application "Console"
	run -- or launch
	activate
end tell

(Nigel Garvey) #3

This works on my (new :yum:) iMac:

tell application "Console" to quit
tell application "System Events"
	repeat while (application process "Console" exists)
		delay 0.2
	end repeat
end tell
tell application "Console" to activate

(Phil Stokes) #4

Thanks for the reply, Nigel.

I’d rather avoid System Events. I’ve had too many experiences of it not responding in the past and can’t take that chance.


(Phil Stokes) #5

Ah, thanks. This seems to work without producing the error message.


(Shane Stanley) #6

What about:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set theApp to (current application's NSRunningApplication's runningApplicationsWithBundleIdentifier:"com.apple.Console")'s firstObject()
set theURL to theApp's its bundleURL()
theApp's forceTerminate()
repeat while theApp's isTerminated() is false
	delay 0.1
end repeat
current application's NSWorkspace's sharedWorkspace()'s launchApplicationAtURL:theURL options:0 configuration:(missing value) |error|:(reference)
tell application id "com.apple.Console" -- resolved at run-time
	-- etc
end tell

(Shane Stanley) #7

Congratulations :ok_hand:

Does this mean new as in running 10.12?


(Phil Stokes) #8

That looks nice, I’ll give it a try.

One thing I’ve never been clear on with the ASObjC bridge, though, is does it matter what ‘current application’ actually refers to? I mean, can it just be any parent process (including launchd itself), or has it got to be some kind of regular “Application” (i.e., a bundle)?

The parent process in my case is going to be osascript. Is the ASObjC bridge still available here (I know: “try it and see!”)?


(Shane Stanley) #9

It should be fine with osascript.


(Phil Stokes) #10

Yup, seems to run just fine. Thanks a bunch. :+1:t3:


(Nigel Garvey) #11

Yep. I’m still making a few adjustments after the “migration” from my old MBP, but no unpleasant surprises so far. I couldn’t see any way during the initial set-up to prevent the machine from being signed up to iCloud and FaceTime, but I think I’ve managed to neutralise them now. The only comments I have so far about Sierra itself are that I’m not at all interested in any of the features mentioned in the “What’s New” tour and Script Editor’s toolbar bug hasn’t been fixed. No doubt there are improvements beneath the bonnet. The iWork ’09 applications weren’t migrated across, but work perfectly well in Sierra after being copied over separately.

The machine itself is gorgeous, silent, and fast! Script Debugger and Xcode now open practically instantly instead of taking just over and just under half a minute respectively. There’s loads of workspace on the 27" screen, the picture’s great, and the Magic Mouse and Keyboard are cute and easy to use. And my favourite Mac store was offering 10% off the price of this particular model last week. :slight_smile:


(Shane Stanley) #12

You sound like a happy punter!

And you forgot the huge improvement in Sierra: no more AppleScript settings corruption :grinning:


(Nigel Garvey) #13

That’s not why I bought the machine, of course. I just needed something to go with a souvenir mouse mat I bought at a heritage railway recently. :wink: