This project contains a bash script that checks if your MacBook is on battery power when connected to certain Wi-Fi networks (like office networks). If it meets both conditions, it'll use text-to-speech to remind you to plug in your laptop and save battery life!
- macOS (tested on macOS Catalina and later)
- Terminal access
- Clone this repository or download the script file.
- Open Terminal and navigate to the directory containing the script.
- Make the script executable:
chmod +x battery-saver.sh
- Open
battery-saver.sh
in a text editor. - Modify the
office_networks
array to include your office Wi-Fi network names:
office_networks=("Eleven Ways" "Home")
To run the script automatically every 15 minutes:
- Open Terminal.
- Edit your crontab file:
crontab -e
- Add the following line (replace
/path/to/script
with the actual path):*/15 * * * * /path/to/script/battery-saver.sh
- Save and exit the editor.
To run the script manually:
./battery-saver.sh
- The script checks if the MacBook is running on battery power.
- It then checks if the current Wi-Fi network matches any in the predefined list of office networks.
- If both conditions are true, it uses the
say
command to speak a reminder message.
- Modify the
speak_message
function to change the reminder text. - Adjust the cron job timing as needed (e.g., change
*/15
to*/5
for every 5 minutes).
- Ensure the script has execute permissions.
- Check that the Wi-Fi interface name (en0) matches your system. Use
networksetup -listallhardwareports
to verify. - Make sure your system volume is audible for the speech alert.
This project is open source and available under the MIT License.
Contributions, issues, and feature requests are welcome. Feel free to check issues page if you want to contribute.
Roel Van Gils