Skip to content

Instantly share code, notes, and snippets.

@what-name
Last active April 24, 2019 06:04
Show Gist options
  • Save what-name/1258305d5fa292d9be126a0ffa8137dd to your computer and use it in GitHub Desktop.
Save what-name/1258305d5fa292d9be126a0ffa8137dd to your computer and use it in GitHub Desktop.
An Applescript application that connects to a bluetooth speaker->starts iTunes->starts playing a playlist automatically
-- works as of MacOS Mojave, iTunes 12
-- TIMESTAMP: 24. Apr. 2019
-- clear start - quit itunes
tell application "iTunes"
quit
end tell
delay 60 -- sometimes iTunes takes a while to quit
-- turn on bluetooth if off
tell application "System Preferences"
quit
delay 1
activate
set the current pane to pane id "com.apple.preferences.Bluetooth"
delay 2 -- pane switching in System Prefs is painfully slow for some reason
tell application "System Events"
tell process "System Preferences"
tell window "Bluetooth"
tell button 3
if name is "Turn Bluetooth On" then click
end tell
end tell
end tell
end tell
quit
end tell
delay 3 -- safety net
-- connect to Bluetooth speaker
activate application "SystemUIServer"
tell application "System Events"
tell process "SystemUIServer"
-- Working CONNECT Script. Goes through the following:
-- Clicks on Bluetooth Menu (OSX Top Menu Bar)
-- => Clicks on device Item
-- => Clicks on Connect Item
set btMenu to (menu bar item 1 of menu bar 1 whose description contains "bluetooth")
tell btMenu
click
tell (menu item "bluetoothSpeakerName" of menu 1)
click
if exists menu item "Connect" of menu 1 then
click menu item "Connect" of menu 1
else
key code 53 -- Close main BT drop down if Connect wasn't present
end if
end tell
end tell
end tell
end tell
delay 8 -- wait for bluetooth connection before playing
set volume output volume 70
tell application "iTunes"
activate
delay 3
set shuffle enabled to true
play playlist "playlistName"
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment