脚本如何实现自动息屏

wen 实用脚本 1

本文目录导读:

脚本如何实现自动息屏

  1. Windows 系统
  2. Linux 系统
  3. macOS 系统
  4. Python 跨平台脚本
  5. 定时自动息屏脚本
  6. 设置屏幕保护(Windows)
  7. 使用第三方工具
  8. 注意事项

Windows 系统

PowerShell 脚本

# 立即息屏
(Add-Type '[DllImport("user32.dll")] public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);' -Name a -Pas) | Out-Null
$handle = (Get-Process explorer).MainWindowHandle
[Win32]::SendMessage($handle, 0x0112, 0xF170, 2)
# 或者使用更简单的方法
rundll32.exe user32.dll,LockWorkStation

使用 nircmd 工具

:: 下载 nircmd 后
nircmd.exe monitor off

Linux 系统

使用 xset 命令

# 立即关闭屏幕
xset dpms force off
# 设置空闲 N 秒后关闭
xset dpms 0 0 60

使用 dm-tool

# Ubuntu/Debian
dm-tool lock

macOS 系统

AppleScript

-- 立即息屏
tell application "System Events"
    do shell script "pmset displaysleepnow"
end tell

命令行

# 立即进入睡眠(包含息屏)
pmset sleepnow
# 仅睡眠显示器
pmset displaysleepnow

Python 跨平台脚本

import os
import platform
def turn_off_screen():
    system = platform.system()
    if system == "Windows":
        # Windows 使用 PowerShell
        os.system("powershell -command \"(Add-Type '[DllImport(\"user32.dll\")] public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);' -Name a -Pas) | Out-Null; $handle = (Get-Process explorer).MainWindowHandle; [Win32]::SendMessage($handle, 0x0112, 0xF170, 2)\"")
    elif system == "Linux":
        # Linux 使用 xset
        os.system("xset dpms force off")
    elif system == "Darwin":  # macOS
        # macOS 使用 pmset
        os.system("pmset displaysleepnow")
if __name__ == "__main__":
    turn_off_screen()

定时自动息屏脚本

Windows 批处理

@echo off
echo 将在5秒后自动息屏...
timeout /t 5 /nobreak
rundll32.exe user32.dll,LockWorkStation

Linux 定时器

#!/bin/bash
# 30分钟后息屏
sleep 1800
xset dpms force off

设置屏幕保护(Windows)

注册表设置

# 设置10分钟无操作后启动屏幕保护
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name "ScreenSaveActive" -Value "1"
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name "ScreenSaveTimeOut" -Value "600"
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name "SCRNSAVE.EXE" -Value "C:\Windows\System32\scrnsave.scr"

使用第三方工具

常用工具:

  • nircmd (Windows)
  • ScreenOff (Windows) - 轻量级
  • xscreensaver (Linux)
  • Amphetamine (macOS)
# 安装 xscreensaver (Linux)
sudo apt install xscreensaver
# 启动时立即锁定
xscreensaver-command -lock

注意事项

  1. 权限问题:某些命令需要管理员/root权限
  2. 系统兼容性:不同发行版/版本可能有差异
  3. 桌面环境:GNOME、KDE等可能需要不同方法
  4. 安全考虑:锁屏比息屏更安全

根据你的具体需求(立即息屏、定时息屏、锁屏等)选择合适的方法即可。

抱歉,评论功能暂时关闭!