返回

彻底释放macOS待机电量:自动关闭蓝牙和Wi-Fi,开启时自动恢复

开发工具

macOS的睡眠模式是一种节能状态,但它并没有完全关闭某些外围设备,如蓝牙和Wi-Fi。这会导致待机时消耗不必要的电量。本文将介绍一种方法,可以在macOS睡眠时自动关闭蓝牙和Wi-Fi,并在唤醒时自动恢复连接。

macOS睡眠模式的局限性

默认情况下,macOS在进入睡眠模式时不会关闭蓝牙和Wi-Fi。这可能会导致不必要的电量消耗,尤其是当你的设备长时间处于睡眠状态时。

自动关闭蓝牙和Wi-Fi的解决方案

为了解决这个问题,我们可以使用名为"pmset"的命令行实用程序。pmset允许我们控制各种与电源相关的设置,包括睡眠期间的设备行为。

要自动关闭蓝牙和Wi-Fi,请打开终端(位于/Applications/Utilities中)并输入以下命令:

sudo pmset -a hibernatemode 25

这将使你的Mac在睡眠时自动关闭蓝牙和Wi-Fi。

要恢复此设置,请使用以下命令:

sudo pmset -a hibernatemode 3

自动恢复蓝牙和Wi-Fi

现在,我们已经配置了Mac在睡眠时自动关闭蓝牙和Wi-Fi,我们需要确保它们在唤醒时自动恢复连接。为此,我们需要创建一个AppleScript脚本。

打开AppleScript编辑器(位于/Applications/Utilities中),粘贴以下代码:

on run
    tell application "System Events"
        repeat until ¬
            exists process "SystemUIServer" ¬
            and ¬
            exists (network setup script)
        end repeat
        
        set the bt_devices to (name of every Bluetooth device)
        repeat with bt_device in bt_devices
            connect Bluetooth device bt_device
        end repeat
        
        delay 10
        tell application "System Preferences"
            activate
            reveal anchor "Wi-Fi" of pane "network" of preference pane "Network"
        end tell
        tell application "System Events"
            tell process "SystemUIServer"
                click pop up button 1 of window "Wi-Fi"
                click menu item "Turn Wi-Fi On" of menu 1 of pop up button 1
            end tell
        end tell
    end tell
end run

保存此脚本为*.scpt文件,例如"AutoConnect.scpt"。

调度脚本

最后,我们需要调度此脚本在每次唤醒Mac时运行。为此,请打开"任务计划程序"(位于/Applications/Utilities中)。

点击左侧栏中的"+"按钮创建一个新任务。在"操作"选项卡中,选择"运行AppleScript",然后点击"选择"按钮选择我们刚刚创建的脚本。

在"调度"选项卡中,选择"在登录时运行"。点击"确定"保存任务。

现在,你的Mac将自动在睡眠时关闭蓝牙和Wi-Fi,并在唤醒时自动恢复连接。这将显著节省电量,延长笔记本电脑的电池续航时间。