【转】 Sleepwatcher:Mac睡眠与唤醒自动执行命令
Sleepwatcher 是一个可以再mac睡眠和唤醒时自动执行命令行脚本的工具,下文两部分分别描述了这个工具的安装与设置方法,在实际的使用中不仅可以执行bash脚本,而且可以执行Applescript命令,使用起来十分方便。
Sleepwatcher is a Mac OS X background daemon that can trigger a script to run when various power related events occur, such as the laptop sleeping or waking up, the screen dimming, or power adapter being connected or disconnected.
On my personal laptop, I have an AppleScript set up within sleepwatcher that mutes the audio each time the laptop goes to sleep. This is to prevent accidental audio playing during meetings and classes.
Setup
Below is setup script for sleepwatcher that I have written that installs it automatically as a system wise daemon.
1 |
|
The script begins with deleting any old version of sleepwatcher on the computer. It them moves onto downloading the package and copying the files into the appropriate places. It loads the launch daemon then creates two files, /etc/rc.sleep
and /etc/rc.wakeup
. These get run at their respective times, when the laptop is going to sleep or waking up.
Configuration
My personal /etc/rc.wakeup
script is shown below. All it does is run the short AppleScript command of setting the volume to zero. To get this functionality on your computer, just copy the code below into your own /etc/rc.wakeup
.
1 |
|
Other possible uses for sleepwatcher include updating a DNS record on wakeup, opening up specific applications for a user, and of course running another script of the user’s choice.