自动调整屏幕亮度的脚本怎么写

wen 实用脚本 2

本文目录导读:

自动调整屏幕亮度的脚本怎么写

  1. Windows 系统(PowerShell 脚本)
  2. Linux 系统(Bash 脚本)
  3. macOS 系统(AppleScript + Shell)
  4. Python 跨平台脚本
  5. 自动运行设置
  6. 使用建议

Windows 系统(PowerShell 脚本)

基于时间自动调整

# auto_brightness.ps1
$currentHour = (Get-Date).Hour
# 根据不同时间段设置亮度
if ($currentHour -ge 6 -and $currentHour -lt 18) {
    # 白天(6:00-18:00):较高亮度
    $brightness = 80
} elseif ($currentHour -ge 18 -and $currentHour -lt 22) {
    # 傍晚(18:00-22:00):中等亮度
    $brightness = 50
} else {
    # 夜晚(22:00-6:00):较低亮度
    $brightness = 20
}
# 设置亮度(需要管理员权限)
$monitor = Get-WmiObject -Namespace root/wmi -Class WmiMonitorBrightnessMethods
$monitor.WmiSetBrightness(1, $brightness)

基于环境光(需要传感器支持)

# 获取环境光传感器数据(如果有)
$lightLevel = Get-WmiObject -Namespace root/wmi -Class WmiMonitorBrightness
# 根据环境光调整
if ($lightLevel -lt 20) {
    $brightness = 30
} elseif ($lightLevel -lt 60) {
    $brightness = 50
} else {
    $brightness = 80
}
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name "Brightness" -Value $brightness

Linux 系统(Bash 脚本)

基于时间自动调整

#!/bin/bash
# auto_brightness.sh
# 获取当前小时
HOUR=$(date +%H)
# 定义亮度值范围(0-1 或 0-100,取决于系统)
if [ $HOUR -ge 6 ] && [ $HOUR -lt 18 ]; then
    # 白天亮度
    BRIGHTNESS=0.8
elif [ $HOUR -ge 18 ] && [ $HOUR -lt 22 ]; then
    # 傍晚亮度
    BRIGHTNESS=0.5
else
    # 夜晚亮度
    BRIGHTNESS=0.2
fi
# 方法1:使用 xrandr(X11)
xrandr --output eDP-1 --brightness $BRIGHTNESS
# 方法2:使用 brightnessctl
brightnessctl set $BRIGHTNESS
# 方法3:直接写入 sysfs
echo $BRIGHTNESS > /sys/class/backlight/intel_backlight/brightness

日出日落时间自适应

#!/bin/bash
# solar_brightness.sh
# 获取日出日落时间(需要安装 sunwait 或类似工具)
SUNRISE=$(sunwait list rise 35.6762 139.6503)  # 东京坐标示例
SUNSET=$(sunwait list set 35.6762 139.6503)
# 计算当前时间与日出日落的差值
CURRENT_TIME=$(date +%s)
SUNRISE_TIME=$(date -d "$SUNRISE" +%s)
SUNSET_TIME=$(date -d "$SUNSET" +%s)
# 根据位置动态调整亮度
if [ $CURRENT_TIME -lt $SUNRISE_TIME ] || [ $CURRENT_TIME -gt $SUNSET_TIME ]; then
    # 夜间
    brightnessctl set 20%
elif [ $CURRENT_TIME -gt $SUNRISE_TIME ] && [ $CURRENT_TIME -lt $((SUNRISE_TIME + 3600)) ]; then
    # 日出后1小时:逐渐增亮
    brightnessctl set 60%
elif [ $CURRENT_TIME -gt $((SUNSET_TIME - 3600)) ] && [ $CURRENT_TIME -lt $SUNSET_TIME ]; then
    # 日落前1小时:逐渐变暗
    brightnessctl set 40%
else
    # 白天
    brightnessctl set 80%
fi

macOS 系统(AppleScript + Shell)

-- auto_brightness.applescript
set currentDate to current date
set hour to hours of currentDate
if hour ≥ 6 and hour < 18 then
    -- 白天亮度 80%
    do shell script "brightness 0.8"
else if hour ≥ 18 and hour < 22 then
    -- 傍晚亮度 50%
    do shell script "brightness 0.5"
else
    -- 夜晚亮度 20%
    do shell script "brightness 0.2"
end if

Python 跨平台脚本

#!/usr/bin/env python3
# auto_brightness.py
import platform
import time
from datetime import datetime
import subprocess
import os
def set_brightness_windows(brightness):
    """Windows 设置亮度"""
    import ctypes
    user32 = ctypes.windll.user32
    user32.SetBrightness(brightness)
def set_brightness_linux(brightness):
    """Linux 设置亮度"""
    try:
        # 尝试使用 brightnessctl
        subprocess.run(["brightnessctl", "set", f"{brightness}%"], check=True)
    except:
        # 备选方案:直接写入文件
        try:
            with open("/sys/class/backlight/intel_backlight/brightness", "w") as f:
                f.write(str(int(brightness * 2.55)))  # 转换为 0-255 范围
        except PermissionError:
            print("请以root权限运行此脚本")
def set_brightness_macos(brightness):
    """macOS 设置亮度"""
    subprocess.run(["brightness", str(brightness)])
def get_best_brightness():
    """根据时间计算最佳亮度"""
    current_hour = datetime.now().hour
    # 定义亮度曲线(使用正弦函数使过渡更平滑)
    import math
    # 假设 6:00 最亮,20:00 最暗
    hour_float = current_hour + datetime.now().minute / 60.0
    # 使用正弦波函数模拟自然光变化
    brightness = 30 + 50 * math.sin((hour_float - 6) * math.pi / 14)
    # 限制范围
    brightness = max(10, min(100, brightness))
    return int(brightness)
def main():
    system = platform.system()
    while True:
        brightness = get_best_brightness()
        if system == "Windows":
            set_brightness_windows(brightness)
        elif system == "Linux":
            set_brightness_linux(brightness)
        elif system == "Darwin":  # macOS
            set_brightness_macos(brightness)
        print(f"已调整亮度至: {brightness}%")
        # 每30分钟检查一次
        time.sleep(1800)
if __name__ == "__main__":
    main()

自动运行设置

Windows - 任务计划程序

:: setup_task.bat
schtasks /create /tn "AutoBrightness" /tr "powershell.exe -File C:\Scripts\auto_brightness.ps1" /sc HOURLY /mo 1

Linux - systemd 服务

# /etc/systemd/system/auto-brightness.service
[Unit]
Description=Auto Brightness Service
[Service]
Type=simple
ExecStart=/usr/local/bin/auto_brightness.sh
Restart=always
[Install]
WantedBy=multi-user.target

使用建议

  1. 选择合适的系统:根据您的操作系统选择对应的脚本
  2. 测试调整范围:找到最适合您眼睛的亮度范围
  3. 考虑环境因素:如果有环境光传感器,可以结合使用
  4. 设置自动启动:确保脚本在系统启动时自动运行
  5. 添加手动控制:保留手动调节的能力以备不时之需

需要我详细解释某个特定部分的实现吗?

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