Skip to content

Instantly share code, notes, and snippets.

@tjluoma
Created January 1, 2021 20:52
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tjluoma/202526420d4b82eb4d078dd4a7380c84 to your computer and use it in GitHub Desktop.
This AppleScript will lock your Mac at 5pm and midnight [see installation info below]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Disabled</key>
<false/>
<key>Label</key>
<string>com.tjluoma.lockscreen</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>-e</string>
<string>tell application "System Events"</string>
<string>-e</string>
<string>keystroke "q" using {control down, command down}</string>
<string>-e</string>
<string>end tell</string>
</array>
<key>RunAtLoad</key>
<false/>
<key>StandardErrorPath</key>
<string>/tmp/com.tjluoma.lockscreen.log</string>
<key>StandardOutPath</key>
<string>/tmp/com.tjluoma.lockscreen.log</string>
<key>StartCalendarInterval</key>
<array>
<dict>
<key>Hour</key>
<integer>0</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
<dict>
<key>Hour</key>
<integer>17</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
</array>
</dict>
</plist>
@tjluoma
Copy link
Author

tjluoma commented Jan 1, 2021

  1. (Optional) Edit the plist to change the times when your Mac will be locked. "Hour 0" is midnight. "Hour 17" is 5 p.m. You can adjust those at will.¹

  2. Save this to ~/Library/LaunchAgents/com.tjluoma.lockscreen.plist

  3. Add /usr/bin/osascript to System Preferences » Security & Privacy » Privacy » Accessibility (click on the + and then nagivate to /usr/bin/osascript so it appears in the list. Make sure it is ✔️ checked.

Add osascript to Accessibility

¹ Use LaunchControl to avoid having to edit launchd plists by hand.

  • Note that this will only work on High Sierra and later as that is when ⌘⌃Q was added as global Lock Screen command

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment