macOS version 12.3.1: how to quit ScreenSaverEngine by using AppleScript

387 Views Asked by At

I use code in mac shell,but it make my computer become black screen: open -a ScreenSaverEngine.app; sleep 5; osascript -e 'tell application "ScreenSaverEngine" to quit'

1

There are 1 best solutions below

3
Robert Kniazidis On

Quitting the Screen Saver is easy, but loginning back will require the user password. You have to grant 2 permissions to Terminal.app as well, when first running the osacript bellow. 1) Permissions to control the System.Events.app, 2) permissions to control computer (that is, to be able to send keystrokes):

osascript -e 'tell application "System Events"
    tell screen saver preferences
        start
        delay 5
        stop
    end tell
    delay 1
    keystroke "YourUserPasswordHere" & return
end tell'

Other way is: uncheck Require password after sleep or screen saver begins in the General tab of Security&Ptivacy pane of System Preferences. Then you don't need login back.

osascript -e 'tell application "System Events"
    tell screen saver preferences
        start
        delay 10
        stop
    end tell
end tell'

Note: you can retain iCloud keychain in the pop up dialog.