Created
March 1, 2025 11:25
-
-
Save swayson/d6e58eb7cd7b8446b55fc14155ec7a25 to your computer and use it in GitHub Desktop.
Enable Disable Bluetooth based on Macbook being opened or not
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script: configure_bluetooth_toggle.sh | |
# Purpose: Automatically toggle Bluetooth on macOS when the laptop lid is opened/closed using sleepwatcher and blueutil. | |
# Prerequisites: | |
# - Homebrew (https://brew.sh) must be installed. | |
# - sleepwatcher and blueutil must be installed via Homebrew (run `brew install sleepwatcher blueutil`). | |
# Usage: | |
# Run this script from the terminal: `bash configure_bluetooth_toggle.sh` | |
# It will create/update .sleep and .wakeup scripts in your home directory and restart the sleepwatcher service. | |
# To undo: | |
# 1. Remove the .sleep and .wakeup files from your home directory: `rm ~/.sleep ~/.wakeup` | |
# 2. Restart sleepwatcher to stop using the scripts: `brew services restart sleepwatcher` or stop it: `brew services stop sleepwatcher` | |
# --- Function Definitions --- | |
check_command_exists() { | |
# Checks if a command is in the PATH. | |
# Args: command_name | |
# Returns: 0 if command exists, 1 if not. | |
command -v "$1" >/dev/null 2>&1 | |
return $? | |
} | |
check_file_content() { | |
# Checks if a file exists and contains the expected content. | |
# Args: filepath, expected_content | |
# Returns: 0 if file exists and content matches, 1 if not or file doesn't exist. | |
if [ -f "$1" ]; then | |
if grep -qF -- "$2" "$1"; then # -F for fixed string, -q for quiet | |
return 0 # File exists and contains content | |
else | |
return 1 # File exists but content doesn't match | |
fi | |
else | |
return 1 # File does not exist | |
fi | |
} | |
create_script_file() { | |
# Creates a script file, writes content, and sets executable permissions. | |
# Args: filepath, content | |
# Returns: 0 on success, 1 on failure. | |
echo -e "$2" > "$1" | |
if [ $? -ne 0 ]; then | |
echo "Error: Failed to write to file: $1" | |
return 1 | |
fi | |
chmod 755 "$1" | |
if [ $? -ne 0 ]; then | |
echo "Error: Failed to set executable permissions on: $1" | |
return 1 | |
fi | |
return 0 | |
} | |
restart_service() { | |
# Restarts a Homebrew service and provides feedback. | |
# Args: service_name | |
# Returns: 0 on successful restart, 1 on failure. | |
echo "Restarting service: $1..." | |
brew services restart "$1" | |
if [ $? -ne 0 ]; then | |
echo "Error: Failed to restart service: $1" | |
return 1 | |
fi | |
echo "Service $1 restarted successfully." | |
return 0 | |
} | |
# --- Dependency Checks --- | |
echo "--- Checking Dependencies ---" | |
if ! check_command_exists blueutil; then | |
echo "Error: blueutil is not installed. Please install it using 'brew install blueutil'" | |
exit 1 | |
fi | |
echo "blueutil found." | |
if ! check_command_exists sleepwatcher; then | |
echo "Error: sleepwatcher is not installed. Please install it using 'brew install sleepwatcher blueutil'" | |
exit 1 | |
fi | |
echo "sleepwatcher found." | |
echo "Dependencies checked successfully." | |
echo "---" | |
# --- Script Content Definition --- | |
sleep_script_path="$HOME/.sleep" | |
wakeup_script_path="$HOME/.wakeup" | |
sleep_command="$(which blueutil) -p 0" | |
wakeup_command="$(which blueutil) -p 1" | |
sleep_script_content="#!/bin/bash\n${sleep_command}\nexit 0" | |
wakeup_script_content="#!/bin/bash\n${wakeup_command}\nexit 0" | |
# --- Create/Update .sleep script --- | |
echo "--- Configuring .sleep script ---" | |
if check_file_content "$sleep_script_path" "$sleep_command"; then | |
echo ".sleep script already configured correctly." | |
else | |
echo "Creating/Updating .sleep script: $sleep_script_path" | |
if create_script_file "$sleep_script_path" "$sleep_script_content"; then | |
echo ".sleep script created/updated successfully." | |
else | |
echo "Error: Failed to create/update .sleep script." | |
exit 1 | |
fi | |
fi | |
echo "---" | |
# --- Create/Update .wakeup script --- | |
echo "--- Configuring .wakeup script ---" | |
if check_file_content "$wakeup_script_path" "$wakeup_command"; then | |
echo ".wakeup script already configured correctly." | |
else | |
echo "Creating/Updating .wakeup script: $wakeup_script_path" | |
if create_script_file "$wakeup_script_path" "$wakeup_script_content"; then | |
echo ".wakeup script created/updated successfully." | |
else | |
echo "Error: Failed to create/update .wakeup script." | |
exit 1 | |
fi | |
fi | |
echo "---" | |
# --- Restart sleepwatcher service --- | |
echo "--- Restarting sleepwatcher service ---" | |
if restart_service sleepwatcher; then | |
echo "sleepwatcher service restarted and is now using the new scripts." | |
else | |
echo "Error: Failed to restart sleepwatcher service. Please check the logs using 'brew services logs sleepwatcher'." | |
exit 1 | |
fi | |
echo "---" | |
# --- Success Message --- | |
echo "Bluetooth automatic toggle on lid close/open configured successfully!" | |
echo " " | |
echo "To undo this configuration:" | |
echo "1. Remove the .sleep and .wakeup files: rm ~/.sleep ~/.wakeup" | |
echo "2. Restart sleepwatcher (or stop it): brew services restart sleepwatcher" | |
echo " " | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment