How Chat GPT helped me automate my Magic Trackpad Workflow on MacOS

How Chat GPT helped me automate my Magic Trackpad Workflow on MacOS

As someone who has never written a line of code in my life, the idea of scripting and automating tasks on a Mac seemed daunting(not to mention I've only owned my first Mac for a few months now, so I'm far from being a Mac power user or anything remotely similar). But recently, I was faced with a situation where I wanted my Apple Magic trackpad to be able to switch between my Mac and my Windows Computer, which I use for work. I am a 3-input kind of guy, so I rely on using a trackpad, a keyboard and a mouse for maximum productivity, but each time I switched devices I had to manually disconnect and reconnect the trackpad.

The Issue: Switching Devices Made Cumbersome by Bluetooth Management

Like many users with dual systems, I wanted to use my Magic Trackpad seamlessly on both macOS and Windows. However, the hassle of manually disconnecting the trackpad from one device and reconnecting it to the other became tiresome, especially when my Mac wouldn't automatically forget the device after I used it on my Windows PC. I needed an efficient way to automate the following:

  1. Automatically "forget" the Magic Trackpad on macOS when it gets disconnected so I can use it on Windows without interference.
  2. Reconnect the Magic Trackpad back to macOS when it becomes available after using it on Windows.

Step 1: Learning the Basics and Setting Up

I started by learning that macOS has a command-line tool called blueutil, which can manage Bluetooth connections. The next step was to install it using Homebrew (a package manager for macOS).

  • To install Homebrew, I ran the following command in Terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

  • Once Homebrew was installed, I installed blueutil by running:

brew install blueutil

With these tools in place, I had what I needed to begin automating the process.

Step 2: Automating the "Forget" Process When Disconnected

The first task was to automatically forget the Magic Trackpad when it gets disconnected from my Mac. This way, I wouldn’t have to manually go into Bluetooth settings and click "Forget" each time I wanted to switch the trackpad to my Windows PC.

Writing My First Script

The solution was a Bash script (a simple program for automating tasks). Here’s the code that automatically checks if my Magic Trackpad is disconnected and, if so, forgets it:

bash

#!/bin/bash

# MAC address of your Magic Trackpad
TRACKPAD_MAC="XX:XX:XX:XX:XX:XX"

# Check if the Magic Trackpad is connected using system_profiler
connected=$(system_profiler SPBluetoothDataType | grep -A 20 "Magic Trackpad" | grep "Connected: Yes")

if [ -z "$connected" ]; then
    # If the trackpad is not connected, unpair it
    echo "Magic Trackpad disconnected, forgetting the device..."
    /opt/homebrew/bin/blueutil --unpair $TRACKPAD_MAC
    echo "Magic Trackpad forgotten"
else
    echo "Magic Trackpad is still connected"
fi        

In this script:

  • system_profiler checks the Bluetooth status of the Magic Trackpad.
  • If the trackpad is disconnected, the script uses blueutil --unpair to forget it.

Making the Script Run Automatically

To make the script run periodically, I used a tool called cron, which schedules tasks on macOS. Here’s how I set it up:

  • I opened Terminal and typed:

crontab -e

  • I added the following line to make the script run every minute:

* /Users/yourusername/forget_trackpad.sh

This ensured that every minute, my Mac would check whether the trackpad was disconnected, and if so, it would automatically forget the device. This made switching between devices much smoother!

Step 3: Reconnecting the Magic Trackpad with a Shortcut

Now that my Mac could automatically forget the trackpad, I needed a way to reconnect it quickly once I was done using it on my Windows PC.

Setting Up a Reconnect Script

I wrote a simple reconnect script to search for my Magic Trackpad and reconnect it when available:

bash

#!/bin/bash

# MAC address of your Magic Trackpad
TRACKPAD_MAC="XX:XX:XX:XX:XX:XX"

# Start scanning for devices
echo "Searching for Magic Trackpad..."
while true; do
    # Scan for the Magic Trackpad's MAC address
    if /opt/homebrew/bin/blueutil --inquiry | grep -q "$TRACKPAD_MAC"; then
        echo "Magic Trackpad found, pairing and connecting..."
        /opt/homebrew/bin/blueutil --pair $TRACKPAD_MAC
        /opt/homebrew/bin/blueutil --connect $TRACKPAD_MAC
        echo "Magic Trackpad connected"
        exit 0
    else
        echo "Still searching..."
    fi
    sleep 5
done
        

This script uses blueutil --inquiry to scan for the trackpad and connects it when it’s found.

Creating a Keyboard Shortcut with Automator

To make things even more efficient, I used Automator to create a Quick Action that would run this script whenever I pressed a keyboard shortcut.

  1. In Automator, I created a Quick Action and added a Run Shell Script block.
  2. I inserted the path to my reconnect_trackpad.sh script and saved it.
  3. Then, I went to System Preferences > Keyboard > Shortcuts and assigned a custom shortcut to my Quick Action.

Now, whenever I press the shortcut, my Mac automatically reconnects to the Magic Trackpad!

Step 4: Reflection on the Process

Now, I know I have some people who actually write codes for a living in my network, and I would be curious to hear their thoughts on my first "coding journey". Surely, I won't be considered for any programmer interview anytime soon, but in the end, I was able to automate my workflow without any prior experience in coding. I now have a solution that works reliably, and I've gained confidence in my ability to perform simple scripting tasks on my Mac.

This wouldn't have been possible years ago, and I honestly didn't ever think it would be possible for me. So, if you're someone who thinks coding is out of your reach, I encourage you to dive in and explore it. We now have the right tools to guide us and to make our lives easier.

要查看或添加评论,请登录

Viorel Sima的更多文章

社区洞察

其他会员也浏览了