Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active September 25, 2024 08:05
Show Gist options
  • Save henrik/3d4c622a5567cdf2bf461352f48ad4dd to your computer and use it in GitHub Desktop.
Save henrik/3d4c622a5567cdf2bf461352f48ad4dd to your computer and use it in GitHub Desktop.
Automatically launch Cuphead (from Steam) on a Mac when a Nimbus Bluetooth game controller is connected.

The only game I play on the Mac Mini connected to our TV is Cuphead.

I wanted the game to launch automatically when I turn on one of our Nimbus game controllers, which connect via Bluetooth.

This is how:

  • Save the .scpt file below as e.g. ~/Library/Scripts/LaunchCupheadIfControllerIsConnected.scpt
    • In the .scpt, change "Nimbus" if your Bluetooth game controllers have some other name.
  • Save the .plist below as e.g. ~/Library/LaunchAgents/henrik.launch_cuphead_if_controller_is_connected.plist
    • In the .plist, change media to whatever your username is.
  • Load the .plist as a launch agent with launchctl load ~/Library/LaunchAgents/henrik.launch_cuphead_if_controller_is_connected.plist

That should be it. When something changes about Bluetooth devices, the launch agent will trigger the script. The script checks if there is a connected device with "Nimbus" in its name (our controllers are named "Nimbus 1" and "Nimbus 2"). If so, it launches Cuphead if not already launched.

On the first run (possibly two runs?) you should get a macOS prompt to confirm permissions.

Works for me(tm) on macOS Mojave.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>henrik.launch_cuphead_if_controller_is_connected</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>/Users/media/Library/Scripts/LaunchCupheadIfControllerIsConnected.scpt</string>
</array>
<key>WatchPaths</key>
<array>
<string>/Library/Preferences/com.apple.Bluetooth.plist</string>
</array>
</dict>
</plist>
use framework "IOBluetooth"
use scripting additions -- https://stackoverflow.com/a/52806598/6962
on isDeviceConnected(substring)
repeat with device in (current application's IOBluetoothDevice's pairedDevices() as list)
if device's isConnected and (device's nameOrAddress as string) contains substring then return true
end repeat
return false
end isDeviceConnected
-- Nimbus game controller
if isDeviceConnected("Nimbus") then
tell application "System Events" to set cupheadIsRunning to ((name of processes) contains "Cuphead")
if not cupheadIsRunning then
-- Steam doesn't seem to like us opening Cuphead.app directly.
open location "steam://run/268910"
end if
end if
@andreabusi
Copy link

I'm not able to make this works on macOS 12.1. It seems that the com.apple.Bluetooth.plist trigger is never called.

I tried to change the ProgramArguments with a sample script, but still no action performed.

Am I missing something?

@henrik
Copy link
Author

henrik commented Dec 14, 2021

Afraid I haven't used this for a while so I'm not sure. If I do I'll try to remember to report back!

@MrSimmmons
Copy link

MrSimmmons commented Sep 22, 2024

I'm, *checks date*, 3 years late. But I'm also trying to set up something else to listen to com.apple.Bluetooth.plist and am also finding that it never triggers. I'm running macOS 14.6.1

@andreabusi / @henrik Were either of you able to get the listening part working?

@henrik
Copy link
Author

henrik commented Sep 24, 2024

Afraid I still haven't used this for an even longer while!

@andreabusi
Copy link

I've never been able to get this script working, or it was unreliable for me.

For what I needed, I used a macOS app called Shortery that runs automations when a particular condition is triggered. In this way I'm able to run a shortcut when a device connect or disconnect from the Mac.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment