To auto-lock your device using a script, you can write different scripts depending on the operating system you’re using. Below are examples for Windows, macOS, and Linux.
1. Windows
For Windows, you can create a script to lock your computer using the built-in rundll32 command.
-
Open Notepad and add the following line:
-
Save the file with a
.batextension, for example,lock.bat. -
Now, running this
.batfile will lock your Windows computer.
You can also schedule this to run at a specific time or after a certain delay using Task Scheduler.
2. macOS
On macOS, you can use a simple AppleScript to lock the screen.
-
Open the Script Editor app on your Mac.
-
Paste the following AppleScript:
-
Save the script as an application (e.g.,
lock.app). -
When you run this app, it will lock your macOS system.
Alternatively, you can run a terminal command to lock the screen:
This will immediately lock your screen by putting the display to sleep.
3. Linux
For Linux, the method depends on the desktop environment you’re using, but a common command is gnome-screensaver-command.
For example, on Ubuntu (GNOME desktop):
This will lock the screen immediately. You can add this command into a shell script:
-
Save the file as
lock.sh, then make it executable:
Now, running lock.sh will lock your Linux system.
Automating with Timed Lock (Optional)
You can also schedule the script to run at specific intervals using task scheduling tools:
-
Windows: Use Task Scheduler.
-
macOS: Use launchd or cron jobs.
-
Linux: Use cron jobs.
If you’d like further help setting up a scheduled task, feel free to ask!