Home/Q&A/Why does my Mac script work while connected but fail after I disconnect?

Why does my Mac script work while connected but fail after I disconnect?

Updated · airankrush

Direct answer

Most likely something to do with the interface. The machine keeps running after you disconnect, but the screen may lock and the display state may change, so scripts that rely on clicks and windows fail; scripts that never touch the interface are not affected.

Basis

  • Screen lock: once the screen is locked, clicks and keystrokes sent through System Events do not reach the target app, and a screenshot shows the lock screen. Check the system's Lock Screen settings first, and whether the remote-access software has an option like “lock after disconnect”.
  • Resolution: some remote-access software changes the remote resolution during a session to suit your local screen and restores it when you disconnect. Scripts that click by screen coordinates will then miss; locate by element name instead.
  • Clipboard: during a session the local and remote clipboards may sync with each other. If the script relies on copy-then-paste, whatever you copied on your own computer during testing gets mixed in, and the behavior differs once you disconnect.
  • Display sleep: after a period with no input the display goes to sleep, and some apps pause rendering their windows. Prefixing the command with caffeinate -d keeps the display awake while that command runs, with no system settings changed.

Information as of .

Boundaries: when the answer is different

If what fails is a command-line script that never touches the interface, the cause is not here; check whether the terminal window was closed, and whether environment variables exist only in the interactive terminal. Before changing system-level lock screen, login or energy settings, check with support: these settings are tied to the delivery configuration and to how the machine is recovered remotely, and changing them wholesale yourself can leave you unable to connect after a restart. Whether the machine has a display or an equivalent device attached, and what resolution the desktop has when nobody is connected, is stated in the delivery notes; we do not guarantee that every kind of UI automation works with nobody connected.

How to do it

  1. Have the script write a log entry before and after each step, and save a screenshot to a file when it fails.
  2. Disconnect, wait for the script to fail, reconnect, and see whether the screenshot shows the lock screen, a black screen or a normal desktop.
  3. Lock screen: check the lock options in the remote-access software and in the system, and ask support before changing system settings.
  4. Clicks landing off target: record the resolution before and after connecting, and change coordinate clicks to locating by element.
  5. Black screen or windows not refreshing: wrap the script in caffeinate -d and try again.

Use case

This question belongs to the use case: iMessage, AppleScript and Shortcuts on a rented Mac mini: it runs, but Apple decides about your account

Other questions in the same use case:

Pricing and availability: see the plans page · Help center (中文) · Refund policy (中文) · Telegram Contact support

Back to home · All Q&A