如何用脚本实现定时静音

wen 实用脚本 2

本文目录导读:

如何用脚本实现定时静音

  1. Windows 系统(PowerShell + 任务计划程序)
  2. macOS 系统(AppleScript + 定时任务)
  3. Linux 系统(命令行 + cron)
  4. Python 跨平台方案
  5. 使用现成软件(推荐)
  6. 使用建议

Windows 系统(PowerShell + 任务计划程序)

创建 PowerShell 静音脚本:

# mute.ps1 - 静音脚本
$wshShell = New-Object -ComObject WScript.Shell
$wshShell.SendKeys([char]173)  # 发送静音/取消静音快捷键
# unmute.ps1 - 取消静音脚本(同样代码)
$wshShell = New-Object -ComObject WScript.Shell
$wshShell.SendKeys([char]173)

设置任务计划:

# 注册静音任务(例如每天22:00)
schtasks /Create /TN "Mute" /TR "powershell.exe -File C:\scripts\mute.ps1" /SC DAILY /ST 22:00
# 注册取消静音任务(例如每天07:00)
schtasks /Create /TN "Unmute" /TR "powershell.exe -File C:\scripts\unmute.ps1" /SC DAILY /ST 07:00

macOS 系统(AppleScript + 定时任务)

创建静音脚本:

-- mute.scpt - 静音
set volume output volume 0
-- unmute.scpt - 取消静音(设为你想要的音量)
set volume output volume 50

使用 crontab 定时执行:

# 编辑定时任务
crontab -e
# 添加以下行(每天22:00静音,07:00取消静音)
0 22 * * * osascript /path/to/mute.scpt
0 7 * * * osascript /path/to/unmute.scpt

Linux 系统(命令行 + cron)

创建静音脚本:

#!/bin/bash
# mute.sh - 静音
# 使用 PulseAudio
pactl set-sink-mute @DEFAULT_SINK@ 1
# 或使用 ALSA
# amixer set Master mute
# unmute.sh - 取消静音
pactl set-sink-mute @DEFAULT_SINK@ 0
pactl set-sink-volume @DEFAULT_SINK@ 50%

设置 cron 任务:

crontab -e
# 每天22:00静音,07:00取消
0 22 * * * /path/to/mute.sh
0 7 * * * /path/to/unmute.sh

Python 跨平台方案

# mute_control.py
import subprocess
import sys
import schedule
import time
def mute_windows():
    subprocess.run(["powershell", "-Command", 
                   "$wshShell = New-Object -ComObject WScript.Shell; $wshShell.SendKeys([char]173)"])
def mute_mac():
    subprocess.run(["osascript", "-e", "set volume output volume 0"])
def mute_linux():
    subprocess.run(["pactl", "set-sink-mute", "@DEFAULT_SINK@", "1"])
def unmute_windows():
    subprocess.run(["powershell", "-Command", 
                   "$wshShell = New-Object -ComObject WScript.Shell; $wshShell.SendKeys([char]173)"])
def unmute_mac():
    subprocess.run(["osascript", "-e", "set volume output volume 50"])
def unmute_linux():
    subprocess.run(["pactl", "set-sink-mute", "@DEFAULT_SINK@", "0"])
# 检测平台
import platform
system = platform.system()
# 定时任务
schedule.every().day.at("22:00").do(mute_windows if system == "Windows" 
                                     else mute_mac if system == "Darwin" 
                                     else mute_linux)
schedule.every().day.at("07:00").do(unmute_windows if system == "Windows" 
                                     else unmute_mac if system == "Darwin" 
                                     else unmute_linux)
while True:
    schedule.run_pending()
    time.sleep(60)

使用现成软件(推荐)

Windows - 使用 HotKeyControl:

# 安装提供命令行控制的软件
choco install hotkeycontrol
# 创建批处理文件实现定时
# 使用 Windows 任务计划程序设置时间

万能方案 - 使用 nircmd(Windows):

# 下载 nircmd 到 PATH
nircmd mutesysvolume 1  # 静音
nircmd mutesysvolume 0  # 取消静音

使用建议

  1. 测试先行:先手动运行脚本测试是否正常工作
  2. 权限注意:确保脚本有执行权限
  3. 异常处理:考虑添加错误日志和重试机制
  4. 灵活调整:可根据需要添加更多时间点或条件判断
  5. 开机自启:如需开机自动运行,可添加开机启动项

选择适合你系统的方案,根据实际需求调整时间和音量参数即可。

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