0

I'm running BigSur 11.7.5.

I want to run some sort of command or script from the command line which will dismiss the screen saver, but only if the screen saver is active. If the screen saver is not active, then I would like this command or script to be an effective no-op.

I intend to invoke the command or script via a non-interactive ssh session when I am not in front of the Mac, and therefore, I never know at invocation time whether or not the screensaver is active when I ssh in to run the command or script.

One of the commonly mentioned ways to dismiss the screensaver is to use osascript to send a click or a newline character. However, if the screensaver is not active, that click or newline could affect the behavior of some existing app which might be open on the Mac's screen.

This is why I'm looking for a way to dismiss the screensaver via the command line if it's active, and perform a no-op if it's not active.

Thank you in advance for any suggestions.

6
  • 1
    What is the use case here?
    – Allan
    Commented Apr 2, 2023 at 23:32
  • The use case is to remotely invoke the following: start up an app, and then control the app via automator. But the screensaver needs to be off in order for automator to properly "see" the app's window in order to control the app. Therefore, I want to force the screensaver to be off before remotely invoking that sequence. However ... I just figured out the answer to my question! I'm in the process of posting that answer here now.
    – HippoMan
    Commented Apr 3, 2023 at 0:36
  • Wouldn’t the screen saver disable the moment you select and App/window?
    – Allan
    Commented Apr 3, 2023 at 1:07
  • Why not use key code 55, i.e. command key. Unlike 'return' or 'click', it shouldn't be treated as an action by any apps (that I'm aware of) but it will stop the screen saver.
    – Mockman
    Commented Apr 3, 2023 at 1:38
  • 1
    Hmm ... I'll try key code 55 when I get a chance. Thank you!
    – HippoMan
    Commented Apr 3, 2023 at 2:20

1 Answer 1

1

I figured out how to do it. I put the following into a shell script ...

osascript -e 'tell application "System Events" to get running of screen saver preferences' | egrep -i true && {                                 
  osascript -e 'tell application "System Events" to keystroke return'                                                                              
}

Then, I just have my non-interactive ssh session run that shell script. If the screen saver is not active, it does nothing; and if the screen saver is running, it gets dismissed.

PS: And now I realize that I also should prefix that with caffeinate -u -t 2 so that the screen will turn on in addition to the running screensaver being dismissed, if necessary.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .