Why 'System events' isn't running?

I have string:


tell application "System Events" to if exists «class prcs» "Excel" then
	tell application "Finder"
		quit application "Microsoft Excel"
	end tell
end if

Some time ago this stopped work properly. After lunching Mac says:
“error “System Events got an error: Application isn’t running.” number -600”

Why it happens? (i suspect, this appears after OS updating).

Thanks.

Not sure, but this might help:

tell application "Finder"
	get name of every process
end tell
tell application "System Events" to activate

wouldn’t hurt.

KOENIG Yvan (VALLAURIS, France) mardi 8 octobre 2013 16:09:10

Firstly, you’ve got the ‘quit’ command inside ‘tell’ statements for two other applications. A recipe for confusion.
Secondly, the Finder’s not needed here anyway.
Thirdly, the process name given is different from the application name. Sometimes that’s correct; usually it’s not. I don’t know the situation with Microsoft Excel as I don’t have it.
Fourthly, System Events itself now quits after a certain amount of time unless you run a script to change this. However, I’d expect it only to cause a slight delay here and your error message suggests it’s actually System Events reporting the error.

Possibly something like this would fix the problem:


tell application "System Events" to set ExcelRunning to ((bundle identifier of application processes) contains "com.microsoft.Excel")
if (ExcelRunning) then quit application id "com.microsoft.Excel"

I’d leave the Finder out of it. This works for me in ML 10.8.5

tell application "System Events" to if exists process "Excel" then
	tell application "Microsoft Excel"
		activate -- added to promote a possible message to save.
		quit
	end tell
end if

Since 10.5 (this is pretty unknown) you can retrieve the running state of an application directly without Finder or System Events


if application "Microsoft Excel" is running then
	quit application "Microsoft Excel"
end if

or

tell application "Microsoft Excel"
	if it is running then quit
end tell

Hi Stefan,

That’s a good one. ‘running’ is a property of every application!

Edited: then you can do something like this:

tell application "LibreOffice"
	if running then quit
end tell

Edited: should have put on my shades :cool:.

Edited: no that wouldn’t work because the tell block would launch the application. Still interesting though.

Thanks,
kel

Adding a short delay might do it also:

tell application "System Events"
	delay 0.5
	if exists process "Excel" then
		quit application "Microsoft Excel"
	end if
end tell

I think I’d rather use the application id though as in Nigel’s script. That probably won’t change.

I edited my process quitting script to use that instead of names:

set my_name to name of me
tell application "System Events"
	set app_ids to (bundle identifier of every process whose visible is true and not (name is my_name or name is "Finder" or name is "System Events"))
end tell
repeat with app_id in app_ids
	tell application id app_id to quit
end repeat

gl,
kel

Hi Stefan,
That’s a very nice and crisp way of writing it :slight_smile:
Is there anywhere a detailed history (“official” or otherwise) of AS changes, updated at each successive AS upgrade? :wink:

Cheers, Chris

It seems to be not useful that to get the ‘running’ property you need to launch the application.

https://developer.apple.com/library/mac/releasenotes/AppleScript/RN-AppleScript/Introduction/Introduction.html

The running property does not require the app to be running.

Thanks, Shane.

The only use for the running property might be to wait until it’s running then. Something like this:

tell application "SomeApp"
	repeat until running
		delay 0.2
	end repeat
end tell

I can think of more situations :wink:

The running state is just a finished launching state. It simply says that the application has fully loaded by the program loader (into the kernel). It doesn’t say if if the application itself is completed with it’s initialization. The AppleEvent manager sends an apple event with a timeout which means the application still has 1 minute to launch. Adding a wait-loop is useless to me, because the running state doesn’t even wait until all the UI elements are loaded (for GUi scripting)

Heavy. I need to reread this tomorrow :slight_smile:

My mistake,

The point of my post was that “waiting until the application is running” is already done by the AppleEvent Manager. When an application is still launching the AppleEvent manager waits until the application is ready to receive AppleEvents. To make an wait-loop for this is not needed. Another situation where you can need a wait-loop is for GUI scripting, making sure the gui is ready. But the application already has an running state even before interface files are loaded so it’s not useful in those situations either.

Because I don’t see a third situation where you ever need such a construct I won’t advise anyone to use that in their code.

This does work so far.

Thanks, Stefan ))
Thanks all!

And because running returns without launching the app, the snippet is effectively an infinite loop anyway…

Had a slightly good sleep and it does work! I was thinking that using the application reference would launch the application, but It only does this when first compiled.

Thanks,
kel