How to send Apple Events to System Events in Mojave (public release)?

Other than running the script directly from Script Editor, is there a way to run a script which can send Apple Events to System Events? I haven’t found a workaround for this yet.

Quitting System Events at the top of the script allows it to run. e.g.

tell application “System Events” to quit
tell application “TextEdit” to activate
delay 0.5
tell application “System Events” to keystroke “Hello”

I don’t know if this is a Mojave bug, or a quirk specific to my computer.

Hi.

Does telling System Events to launch work at the top of the script, instead of telling it to quit? System Events does quit anyway after five minutes of non-use, which may explain the problem you were having. It’s possible to change the timeout interval by setting System Events’s quit delay property to the preferred number of seconds. Or it can be set to 0 to prevent the timeout from happening at all. I have a log-in script app which includes this code:


-- Launch System Events and set it never to quit.
using terms from application "System Events"
	tell application ((path to "csrv" as text) & "System Events.app")
		launch
		set quit delay to 0
	end tell
end using terms from

I wrote it in this long-winded way five years ago to get round a “Where is System Events?” error I was getting at the time. I don’t know if the long-windedness (ie. using terms from and the path to the application) is still necessary. :slight_smile:

Thanks for your suggestion, Nigel.

I had noticed that System Events seemed to be always running on my system, and suspected it was becoming somewhat unresponsive after a while, hence a need to quit it before calling it.

So it doesn’t look like telling System Events to launch would help.

I used your script to check the quit delay setting, which was 300, then set it to 5 seconds. Then I watched it in Activity Monitor, but it didn’t quit after 5 seconds. There may be some background process keeping it open, I suppose.

This is early days for Mojave, so I can be patient now that I’ve found this workaround.