Skip to content

Instantly share code, notes, and snippets.

@ieatfood
Forked from jaredmoody/Connect Airpods.applescript
Last active April 17, 2025 05:03
Show Gist options
  • Save ieatfood/814b065964492f71f728da59a47413bc to your computer and use it in GitHub Desktop.
Save ieatfood/814b065964492f71f728da59a47413bc to your computer and use it in GitHub Desktop.
An Applescript to connect bluetooth devices, such as Airpods. Nice when paired with an alfred trigger.
# Taken from https://www.reddit.com/r/MacOS/comments/i4czgu/big_sur_airpods_script/gck3gz3/
# by https://github.com/smithumble
use framework "IOBluetooth"
use scripting additions
set AirPodsName to "AirPods"
on getFirstMatchingDevice(deviceName)
repeat with device in (current application's IOBluetoothDevice's pairedDevices() as list)
if (device's nameOrAddress as string) contains deviceName then return device
end repeat
end getFirstMatchingDevice
on toggleDevice(device)
if not (device's isConnected as boolean) then
device's openConnection()
return "Connecting " & (device's nameOrAddress as string)
else
device's closeConnection()
return "Disconnecting " & (device's nameOrAddress as string)
end if
end toggleDevice
return toggleDevice(getFirstMatchingDevice(AirPodsName))
@rozavon
Copy link

rozavon commented Apr 5, 2023

yes. Here is my code
image

@penn201500
Copy link

penn201500 commented Apr 20, 2023

Great! I can connect to my specific Bluetooth headset. And to disconnect one specific Bluetooth device, you can use the script:

use framework "IOBluetooth"
use scripting additions

set AirPodsName to "HUAWEI FreeBuds Pro"

on getFirstMatchingDevice(deviceName)
	repeat with device in (current application's IOBluetoothDevice's pairedDevices() as list)
		if (device's nameOrAddress as string) contains deviceName then return device
	end repeat
end getFirstMatchingDevice

on disconnectDevice(device)
	set quotedDeviceName to quoted form of (device's nameOrAddress as string)
	if (device's isConnected as boolean) then
		with timeout of 30 seconds
			set deviceValueInAudioSource to ""
			repeat while deviceValueInAudioSource = ""
				set deviceValueInAudioSource to do shell script "/usr/local/bin/SwitchAudioSource -a | grep -m1 " & quotedDeviceName
			end repeat
			
			set currentDeviceName to do shell script "/usr/local/bin/SwitchAudioSource -c"
			if currentDeviceName contains quotedDeviceName then
				do shell script "/usr/local/bin/SwitchAudioSource -n"
			end if
			
			device's closeConnection()
			return "Disconnecting " & (device's nameOrAddress as string)
		end timeout
	end if
end disconnectDevice

set matchingDevice to getFirstMatchingDevice(AirPodsName)
if matchingDevice is not equal to missing value then
	return disconnectDevice(matchingDevice)
else
	return "Device not found"
end if

I've tested it on my Mac osx 13.3.1, it's working well.

@penn201500
Copy link

yes. Here is my code image

got the same error info here, and it turns out my device name is wrong. After I corrected the device name, the shortcut worked well

@bryanrmq
Copy link

What a beauty @penn201500 ! Thanks !

@rgjurgens
Copy link

On Ventura 13.4, the installation folder of SwitchAudioSource is /opt/homebrew/Cellar/switchaudio-osx/1.2.2/bin/SwitchAudioSource, so adjust that path

@jaygooby
Copy link

jaygooby commented Aug 8, 2023

Nice one, thanks!

@jonskoglund
Copy link

I want to run the applescript code @penn201500 posted from the dock as a shortcut but I get an error:
Screenshot 2023-12-04 at 16 43 50
Any idea how to fix this?

@ldantur
Copy link

ldantur commented Dec 6, 2024

Here's a version that works in Sequoia:

use framework "IOBluetooth"
use scripting additions

set AirPodsName to "AirPods" -- Replace with the exact name of your AirPods

on getFirstMatchingDevice(deviceName)
	set pairedDevices to current application's IOBluetoothDevice's pairedDevices() as list
	repeat with device in pairedDevices
		if (device's nameOrAddress as string) contains deviceName then
			return device
		end if
	end repeat
	return missing value
end getFirstMatchingDevice

on connectDevice(device)
	if device's isConnected() as boolean then
		return "Device is already connected: " & (device's nameOrAddress as string)
	else
		device's openConnection()
		delay 2 -- Wait a bit for the connection to establish
		if device's isConnected() as boolean then
			return "Successfully connected to " & (device's nameOrAddress as string)
		else
			return "Failed to connect to " & (device's nameOrAddress as string)
		end if
	end if
end connectDevice

-- Main Execution
set matchingDevice to getFirstMatchingDevice(AirPodsName)
if matchingDevice is not equal to missing value then
	set result to connectDevice(matchingDevice)
	return result
else
	return "Device not found. Ensure your AirPods are paired and the name matches."
end if

@jorgeacruz
Copy link

Connect + Change the audio output device to Airpords

There is a popular problem when AirPods: when they switch from MacBook to iPhone, sometimes they don't connect back. Even if they get reconnected, the audio output still does not get changed back to AirPods.

I've adjusted @ieatfood implementation:

  1. Always try to connect, never try to disconnect.
  2. Use switchaudio-osx (brew install switchaudio-osx or port install switchaudio-osx) to change the audio output device to AirPods
  3. Add to Automator as a Quick Action (see screenshot below) + Apple manual
  4. Add the Quick Actions button to the Touch Bar

Now I have a TouchBar button that instantly gives my sound back.

use framework "IOBluetooth"
use scripting additions

set AirPodsName to "AirPods Pro (Дмитрий)"

on getFirstMatchingDevice(deviceName)
	repeat with device in (current application's IOBluetoothDevice's pairedDevices() as list)
		if (device's nameOrAddress as string) contains deviceName then return device
	end repeat
end getFirstMatchingDevice

on toggleDevice(device)
	set quotedDeviceName to quoted form of (device's nameOrAddress as string)
	
	if not (device's isConnected as boolean) then
		device's openConnection()
	end if
	
	do shell script "/usr/local/bin/SwitchAudioSource -s " & quotedDeviceName
	return "Connecting " & (device's nameOrAddress as string)
end toggleDevice

return toggleDevice(getFirstMatchingDevice(AirPodsName))

image

Woowwww.....it's working! Thanks

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