Categories: MacOS Automation, Bluetooth Device Management, Keyboard Sharing on Mac

If you’re using multiple Mac computers and want to switch your Mac keyboard between Macs, you’ve likely faced the frustration of manually disconnecting and reconnecting your Bluetooth keyboard each time you switch devices. This tedious process can disrupt your workflow and consume valuable time.

In this guide, we’ll show you how to seamlessly switch your Mac keyboard between Macs using a simple script. By automating the disconnection and reconnection process—and incorporating a quick power cycle of your keyboard—you can enhance your productivity and enjoy a smoother multi-Mac setup.

The Challenge of Sharing a Mac Keyboard Between Macs

Apple’s Bluetooth keyboards are designed for effortless pairing with a Mac, but they lack an easy method for switching between multiple Macs. Typically, when you want to use your keyboard with a different Mac, you have to:

  1. Forget the device on the current Mac.
  2. Pair the device on the new Mac.

Repeating these steps every time you switch devices can be a hassle, especially if you frequently alternate between a desktop and a laptop.

The Solution: Automate Keyboard Switching with a Script

By utilizing a shell script and a command-line utility called blueutil, you can automate the process of unpairing and pairing your Bluetooth keyboard between Macs. This script will:

  • Unpair the keyboard from the current Mac.
  • Pair the keyboard with the target Mac.
  • Note: Before running the script, you must turn your keyboard off and then back on. This ensures a clean connection and helps the script work effectively.

Prerequisites

Before proceeding, ensure you have the following installed on your Macs:

  • Homebrew: A package manager for macOS. Install it with:/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  • blueutil: A command-line utility to manage Bluetooth devices. Install it using Homebrew: brew install blueutil

Step 1: Find Your Keyboard’s MAC Address

Each Bluetooth device has a unique MAC address (e.g., 00-12-6f-e6-ae-7e). You’ll need this address to specify which device to disconnect and reconnect.

To find your keyboard’s MAC address:

  1. Open Terminal.
  2. Run the command: blueutil --paired

Output example:

address: 00-12-6f-e6-ae-7e, not connected, not favourite, paired, name: "My MacKeyboard", recent access date: 2024-10-01 02:46:15 +0000

Step 2: Create the Keyboard Switching Script

Create a shell script that automates the Bluetooth disconnection and reconnection.

  1. Open a text editor (e.g., TextEdit, Visual Studio Code).
  2. Paste the following script:
#!/bin/bash

# Bluetooth MAC address of the keyboard
DEVICE="00-12-6f-e6-ae-7e"
# Prompt to turn off and on the keyboard
echo "Please turn off your keyboard, then turn it back on."
read -p "Press Enter to continue after you've cycled the power..."

# Disconnect the keyboard
echo "Disconnecting the keyboard..."
blueutil --unpair $DEVICE

# Wait to ensure proper disconnection
sleep 2

# Reconnect the keyboard
echo "Reconnecting the keyboard..."
blueutil --pair $DEVICE

echo "Keyboard switch complete!"
  1. Replace "00-12-6f-e6-ae-7e" with your keyboard’s MAC address.
  2. Save the file as switch_keyboard.sh.

Step 3: Make the Script Executable

  1. Open Terminal.
  2. Navigate to the script’s directory: cd /path/to/your/script
  3. Make the script executable: chmod +x switch_keyboard.sh

Step 4: Run the Script to Switch Your Keyboard

Whenever you want to switch your keyboard to the current Mac:

  1. Turn off your keyboard and then turn it back on. This step is crucial for the script to work effectively.
  2. Open Terminal.
  3. Run the script: ./switch_keyboard.sh
  4. Follow any on-screen prompts.

The script will unpair the keyboard from any other Mac and pair it with the current one.

Optional: Simplify Script Execution

To make the process even more convenient, you can add the script to your system’s PATH or create an alias.

Option 1: Add to PATH

  1. Move the script to a directory in your PATH (e.g., /usr/local/bin):sudo mv switch_keyboard.sh /usr/local/bin/switch_keyboard
  2. Now, you can run the script from anywhere:switch_keyboard

Option 2: Create an Alias

  1. Open your shell configuration file (e.g., ~/.bash_profile, ~/.zshrc).
  2. Add the alias:alias switchkeyboard='/path/to/your/script/switch_keyboard.sh'
  3. Reload your shell configuration: source ~/.bash_profile
  4. Use the alias to run the script: switchkeyboard

Tips for Successful Keyboard Switching

  • Turn Off and On the Keyboard: Always power cycle your keyboard before running the script. This helps reset the Bluetooth connection and ensures the script works properly.
  • Ensure Bluetooth is Enabled: Make sure Bluetooth is turned on in your Mac’s settings.
  • First-Time Pairing: The first time you pair the keyboard with a Mac, you might need to confirm the connection or enter a passcode.
  • Use for Other Devices: This method can also switch other Bluetooth devices like mice or trackpads by using their MAC addresses.

Troubleshooting Common Issues

  • Keyboard Not Connecting: If the keyboard doesn’t connect, try increasing the sleep duration in the script to give the device more time to disconnect.
  • Permission Denied Error: Ensure the script has execute permissions (chmod +x switch_keyboard.sh) and that you have the necessary permissions to move files to system directories.
  • Script Doesn’t Run: Double-check that you’ve turned the keyboard off and on before running the script.

Conclusion

By automating the Bluetooth connection process with a simple script—and remembering to power cycle your keyboard before each use—you can effortlessly switch your Mac keyboard between multiple Macs. This solution saves time and reduces the hassle of manual disconnection and reconnection, allowing you to maintain a seamless workflow.

Implement this script today to enhance your multi-Mac experience and boost your productivity!


Note: This guide is intended for users comfortable with the command line. Always exercise caution when running scripts that affect system settings.

Additional Resources

Catégorisé: