Swyx Desktop for macOS: Show everybody, that you are in a call via Philips Hue using AppleScript :)
Hi All,
here's a small script monitoring the current user's state via the new & fancy scripting functionality of the Swyx Desktop for macOS Client. Each time the user is in a call, it sets a Philips Hue light to red. If the call is ended, the Philips Hue light is switched off again.
Nice, if you are in your (home) office and don't want to be interrupted by your (wife ;)) / colleagues ;)
What do you need:
- It's a good idea to register a Philips Hue Developer account. It's for free and you will get some nice introductions and samples: https://developers.meethue.com/
- Next you need a "username" to access your Philips Hue Bridge in your network and its API to switch the lights on and off. The details are described here: https://developers.meethue.com/develop/get-started-2/#so-lets-get-started
- Of course you should know the IP address of your Philips Hue Bridge. You can look at your DHCP server which IP address was assigned, or follow the instructions of this site: https://developers.meethue.com/develop/application-design-guidance/hue-bridge-discovery/
- If you have more than 1 Philips Hue light, you need to find out which one you want to control (switch on/off). Here you find the description how to determine your available lights and their appropriate IDs/number: https://developers.meethue.com/develop/hue-api/lights-api/#get-all-lights
- Some knowledge about the new scripting functionality of the Swyx Desktop for macOS is helpful, of course. Take a look here: https://help.swyx.com/ngclients/1.00/Mac/Swyx/en-US/index.html#page/help%2Fchap_scripts.html
That's it - now you are ready to go :)
Short explanation how the AppleScript works:
- First some information about your network environment is set to variables. This information needs to be entered by yourself
- The script will check if the Swyx Desktop client is running. If it's not running or if the client is quit, the script will end automatically
- The script is running a loop and polls the state of the user (because currently there is no event mechanism available to get state changes - but may follow in the future - stay tuned). Each time the state switches to "active" the Philips Hue light will be switched on and set to red. curl is used to call the Philips Hue API. For all other states the Philips Hue light will be switched off
- The script will query the state every second
You can enter the following script source code via the Apple ScriptEditor. Details regarding handling the ScriptEditor are described in the Online Help of the client: https://help.swyx.com/ngclients/1.00/Mac/Swyx/en-US/index.html#page/help%2Fchap_scripts.html
If you want to start the script from the terminal (or automatically) you can call it this way:
"osascript MyFancyScript.scpt"
And finally the source code of the script - first as screenshots to keep syntax highlighting/formatting, because formatting capabilities with this community are quite limited, but at the bottom you will also find a download link of the script for your convenience :)
Part to be customized by you (set IP address of Philips Hue Bridge, username and number of light):
The loop where the work is done:
Script as plain code:
-- switch light on and set saturation, brightness and hue (color), details: https://developers.meethue.com/develop/hue-api/lights-api/#set-light-state
set sLightOn to "'{\"on\":true, \"bri\":254, \"sat\":254, \"hue\": 65535}'"
-- switch light off
set sLightOff to "'{\"on\":false}'" --
-- IP address of Hue Bridge
set sAddress to "192.168.1.11"
-- "username" to access Hue Bridge API, details: https://developers.meethue.com/develop/get-started-2/#so-lets-get-started
set sUsername to "alksdjaasdjakfmjkjaskldfld"
-- number of light to switch on/off, details: https://developers.meethue.com/develop/hue-api/lights-api/#get-all-lights
set sLightNo to "2"
set curlCmdLightOn to "-H \"Accept: application\\json\" -X PUT --data " & sLightOn & " http://" & sAddress & "/api/" & sUsername & "/lights/" & sLightNo & "/state"
set curlCmdLightOff to "-H \"Accept: application\\json\" -X PUT --data " & sLightOff & " http://" & sAddress & "/api/" & sUsername & "/lights/" & sLightNo & "/state"
set swyxDesktopActive to 1
set currentState to ""
set oldState to ""
repeat while swyxDesktopActive > 0
-- check if Swyx Desktop app is still running and end the repeat while loop, if not
tell application "System Events"
set swyxDesktopActive to count (every process whose displayed name is "Swyx Desktop")
end tell
if swyxDesktopActive > 0 then
tell application "Swyx Desktop"
set currentState to state
end tell
if currentState is equal to "active" then
if oldState is not equal to "active" then
do shell script "curl " & curlCmdLightOn & ""
end if
else
if oldState is equal to "active" then
do shell script "curl " & curlCmdLightOff & ""
end if
end if
set oldState to currentState
end if
-- wait a second
delay 1
end repeat
Download link to script:
https://cloud.swyx.net/index.php/s/nYoHsS9VkjLVpsu
Enjoy,
Stefan
Bitte melden Sie sich an, um einen Kommentar zu hinterlassen.
Kommentare
0 Kommentare