Skip to content

Instantly share code, notes, and snippets.

@mangtronix
Created April 13, 2024 06:13
Show Gist options
  • Save mangtronix/b26b462e7872326d75c3175a71352940 to your computer and use it in GitHub Desktop.
Save mangtronix/b26b462e7872326d75c3175a71352940 to your computer and use it in GitHub Desktop.
Automatically click Connect for a BLE MIDI device on MacOS
-- Automatically click Connect for a BLE MIDI device
-- Runs in a loop - hit stop to end
-- The Bluetooth Configuration window must be open
-- Audio MIDI Setup->MIDI Studio->Open Bluetooth Configuration
-- Enter the device name below and run the script
-- Accessibility control must be enabled for the script to click the button
-- Written with ChatGPT
-- Michael Ang @mangtronix
-- 2024-04
property deviceNameToConnect : "MusicDevices" -- Set your device name here
property repeatInterval : 1 -- Set the repeat time interval in seconds here
property alreadyConnectedLogged : false -- Tracks if "already connected" has been logged
on run
tell application "Audio MIDI Setup" to activate -- Ensure the application is active
delay 2 -- Wait a couple of seconds for the application to come to the foreground
repeat
tell application "System Events"
tell application process "Audio MIDI Setup"
-- Check if the window exists
if exists window "Bluetooth Configuration" then
-- set frontmost to true
tell window "Bluetooth Configuration"
tell scroll area 1
tell table 1
set deviceFound to false
set buttonClicked to false
repeat with i from 1 to count of rows
set currentRow to row i
-- Check if the row contains the device name specified by `deviceNameToConnect`
if value of static text 1 of UI element 1 of currentRow is deviceNameToConnect then
set deviceFound to true
-- Check if the cell has a "Connect" button
tell UI element 3 of currentRow -- assuming the button is in the third column
set buttonList to every button
if buttonList is not {} and (name of button 1 is "Connect") then
click button "Connect"
set buttonClicked to true
log "Connecting"
exit repeat -- Button was found and clicked, no need to continue
else if not alreadyConnectedLogged then
-- Log the message only once
log "Device already connected"
set alreadyConnectedLogged to true
end if
end tell
end if
end repeat
-- Output the result if the button was not clicked
if deviceFound and not buttonClicked and not alreadyConnectedLogged then
log "Device found but 'Connect' button not available."
else if not deviceFound and not alreadyConnectedLogged then
log "Device not found."
end if
end tell
end tell
end tell
else
log "Bluetooth Configuration window not found. Trying again."
end if
-- Wait for the specified repeat interval before repeating the loop
delay repeatInterval
end tell
end tell
end repeat
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment