Category filter

Script to enable password for screen lock on Mac

Strong passwords ensure protection against unauthorized access to your macOS devices. Administrators can remotely set the Mac to require a password when it wakes from sleep with the help of scripts in Hexnode. Enabling a password will keep the data on the Mac secure when users are away from it. Hexnode helps admins perform this action with the help of Execute Custom Script feature.

Scripting language – Bash

File extension – .sh

Disclaimer:


The sample scripts provided below are adapted from third-party open-source sites.


Note:


The script is supported only for macOS versions between 10.12 to 12.7.

Enable password for screen lock

The below code uses the osascript command to run an AppleScript line:

osascript –e – Runs the main AppleScript command from within the Shell environment. The ‘osascript’ command with the ‘-e’ parameter is used to execute the AppleScript command directly within a shell environment.

tell application "System Events" to set require password to wake of security preferences to true – Use System Events to automatically enable the ‘Require password __ after sleep or screensaver begins’ checkbox under System Preferences > Security & Privacy > General without any prompts. When the option is enabled on the device, the delay for ‘Require password __ after sleep or screensaver begins’ is set to immediately. When the device goes to sleep or when the screensaver is activated, the device enables the password immediately.

What happens at the device end?

After executing the script from the portal, it enables the Require password after sleep or screen saver begins settings under System Preferences > Security & Privacy > General.

After executing the script, the option 'Require Password after sleep or screen saver begins' is enabled

If the System Preferences is kept open on the device when the script is executed from the portal, you may have to exit and open the System Preferences again for the script to take effect.

Notes:

  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository