Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josefglatz/b54f0050a82dc6bd404f to your computer and use it in GitHub Desktop.
Save josefglatz/b54f0050a82dc6bd404f to your computer and use it in GitHub Desktop.
Toggle Bluetooth WAN Connection
on alfred_script(q)
-- Idea from https://coderwall.com/p/fyfp0w
-- automatic connect/disconnect (added 2014-08-25)
-- disable wifi on connect (added 2014-09-01)
-- Some required settings to get this connect script working
-- Set target bluetooth device
-- Set label of item to "click"
set DeviceToConnect to "jousch.com"
set ConnectLabel to "Mit Netzwerk verbinden"
set DisconnectLabel to "Vom Netzwerk trennen"
## set ConnectLabel to "Connect"
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 target "DeviceToConnect" Item
-- => Clicks on Connect Item
set btMenu to (menu bar item 1 of menu bar 1 where description is "bluetooth")
tell btMenu
click
tell (menu item DeviceToConnect of menu 1)
click
if exists menu item ConnectLabel of menu 1 then
click menu item ConnectLabel of menu 1
display notification "" with title "Connecting to phone" subtitle DeviceToConnect
set wifi to (do shell script "networksetup -listallhardwareports | grep -A 1 Wi-Fi | tail -n 1 | cut -b 9-12")
if (offset of "On" in (do shell script "networksetup -getairportpower " & wifi & " ")) > 0 then
do shell script "networksetup -setairportpower " & wifi & " off"
end if
else if exists menu item DisconnectLabel of menu 1 then
click menu item DisconnectLabel of menu 1
display notification "" with title "Disconnecting from phone" subtitle DeviceToConnect
else
click btMenu -- Close main BT drop down if anything else is wrong
display notification "Are you already connected?" with title "Failure while Connecting/Disconnecting" subtitle "No valid item label was not found"
end if
end tell
end tell
end tell
end tell
end alfred_script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment