Odd Behavior with a Remote Script

Hi everyone,

I’m encountering a weird issue with an Intel Mac mini running El Capitan. The Intel Mac mini is on my network, and I’m running this script from my M1 Mac mini.

set desiredKey to "N"

tell application "System Events" of machine "eppc://macmini.local"
	keystroke desiredKey
end tell

The odd behavior is that if the script isn’t run in a while, it returns an error -600 “System Events got an error: Application isn’t running.”

I found a workaround by accident. If first tell the Finder to set the output volume, and THEN send the keystroke, I never get the error -600, no matter how long it’s been since I ran the script.

set desiredKey to "N"

-- We need the following "tell" for the System Events not to return a -600
tell application "Finder" of machine "eppc://macmini.local"
get volume settings
end tell

tell application "System Events" of machine "eppc://macmini.local"
	keystroke desiredKey
end tell

It’s almost as though something on the Intel Mac mini (appleeventsd?) goes dark after being active for a while, and somehow just getting the volume settings from the Finder wakes things up.

Does this look remotely familiar to anyone?

Hi. I don’t have any personal experience of scripting remote machines. But one thing that comes to mind is that System Events quits after a certain amount of time if it’s not used. Its timeout interval can be changed by setting its ‘quit delay’ property to a preferred number of seconds. A value of 0 stops it auto-quitting altogether. I use a log-in script to achieve this. An alternative to try in your script might be to send the remote System Events a ‘launch’ command before sending the ‘keystroke’ one.

The above’s my best guess based on your error message. But it’s hard to see how telling the Finder to execute a StandardAdditions command would fix the problem. :thinking:

Another point to note is that the process receiving a keystroke must be frontmost and focused at the time.

Thanks for responding.

I tried this script as an alternative:

tell application "System Events" of machine "eppc://macmini.local"
launch
keystroke desiredKey
end tell

The launch doesn’t help. It still fails after some time.

Do you know where the System Events’ ‘quit delay’ is located?

It’s a property that’s set by script:

tell application "System Events"
	launch
	set its quit delay to 0
end tell

It only needs to be set once in any one session and remains in force until you either change it, log out, or shut down.

Thanks for that hint! I’ll employ it on the Mac mini I’m remoting into as a startup script.

One additional weird thing I’m seeing that I can’t figure out:

set remoteMachine to "eppc://macmini.local"
set desiredKey to "l"

-- We need the following "tell" for the System Events not to return a -600
tell application "Finder" of machine remoteMachine
	get volume settings
end tell

-- This is why we are here
tell application "System Events" of machine remoteMachine
	keystroke desiredKey
end tell

Note that I set a variable called remoteMachine to the eppc URL. If I run this script, I get an error on the last tell line:

Screen Shot 2021-12-13 at 12.31.17 PM

But if I pass the contents of remoteMachine as a literal to that last tell line, it works without issue. This is truly weird.

It looks like a compilation problem. In the context of your script, the compiler needs to access the System Events on the remote machine in order to find out that application’s token for ‘keystroke’. It can’t do this if the machine url’s in a variable because variables aren’t set or read until the script’s actually run. ‘keystroke’ still compiles, but the token (as shown in Script Debugger’s “Raw (Chevron) Syntax” view) is different from the System Events one and presumably belongs to something else the compiler can see.

What will probably work is to compile that section against the System Events on the compiling machine:

using terms from application "System Events"
	tell application "System Events" of machine remoteMachine
		keystroke desiredKey
	end tell
end using terms from

The ‘get volume settings’ command doesn’t belong to the Finder but to the StandardAdditions OSAX, so it’s possible you wouldn’t need to use ‘using terms from’ for that section.

I modified my script as you suggested and indeed, it works. Fantastic! Thank you Nigel. These are the hidden gotchas about AppleScript that aren’t necessarily obvious. Thank you again for taking the time to help me understand this.

I also brushed up on the ‘using terms from’ syntax and found this interesting explanation.