Went down the path of trying to trigger monitor source changes for my laptop via home assistant. Realized that it could be used for a bunch of different use cases so I thought I would share.
Basically the way this works is you create an AppleScript Application that is always running in the background and reading notifications searching for specific keywords. You trigger it by sending a notification to your laptop (via the HA App) that matches your keyword.
Full disclosure I wrote none of this and did nothing but copy and paste from a bunch of different places and make small edits to make it work, that said, it works.
My below example contains 3 customizable keyword matches in " " after lookForThisText. This can be expanded to more if you simply add additional keywords with unique identifiers and add additional “ElseIf” statements.
App I am using to change monitor source is call DDCCTL: https://github.com/kfix/ddcctl
Main Inspiration for the code below: https://apple.stackexchange.com/questions/402539/applescript-read-and-trigger-other-script-base-on-received-notification
** Reviewing this you can copy and paste the way he triggers external scripts, I am just running terminal commands**
Need to save the below Applescript as a run “Stay Open” Application and then grant it accessibility permissions and ideally have it run at startup*
property lookForThisText1 : "Change the source to HDMI1" -- your search term
property lookForThisText2 : "Change the source to HDMI2" -- your search term
property lookForThisText3 : "Change the source to USBC" -- your search term
property theseTitles : {}
repeat
getNotificationTitles()
if theseTitles contains lookForThisText1 then
---------------------------------------------------------------
--tell current application to beep 5 -- Just For Testing
do shell script "/Users/***/Downloads/bin/release/ddcctl -d 1 -i 17" -- Your Action Here
tell application "System Events"
tell process "NotificationCenter"
set windowCount to count windows
repeat with i from windowCount to 1 by -1
if description of image 2 of window i is "Home Assistant" then
click button "Close" of window i
end if
end repeat
end tell
end tell
---------------------------------------------------------------
else if theseTitles contains lookForThisText2 then
---------------------------------------------------------------
--tell current application to beep 5 -- Just For Testing
do shell script "/Users/***/Downloads/bin/release/ddcctl -d 1 -i 18" --Your Action Here
tell application "System Events"
tell process "NotificationCenter"
set windowCount to count windows
repeat with i from windowCount to 1 by -1
if description of image 2 of window i is "Home Assistant" then
click button "Close" of window i
end if
end repeat
end tell
end tell
---------------------------------------------------------------
else if theseTitles contains lookForThisText3 then
---------------------------------------------------------------
--tell current application to beep 5 -- Just For Testing
do shell script "/Users/***/Downloads/bin/release/ddcctl -d 1 -i 19" --Your Action Here
tell application "System Events"
tell process "NotificationCenter"
set windowCount to count windows
repeat with i from windowCount to 1 by -1
if description of image 2 of window i is "Home Assistant" then
click button "Close" of window i
end if
end repeat
end tell
end tell
---------------------------------------------------------------
end if
delay 1
end repeat
on getNotificationTitles()
try
-- This Gets The Titles Of The Currently Displaying Notification Alerts And Banners
tell application "System Events"
tell process "Notification Center"
set theseWindows to every window whose subrole is "AXNotificationCenterAlert" or subrole is "AXNotificationCenterBanner"
set theseTitles to {}
repeat with thisWindow in theseWindows
try
set titleText to the value of static text 1 of thisWindow
set the end of theseTitles to titleText
end try
try
set subTitleText to the value of static text 1 of scroll area 1 of thisWindow
set the end of theseTitles to subTitleText
end try
try
set notificationText to the value of static text 2 of scroll area 1 of thisWindow
set the end of theseTitles to notificationText
end try
end repeat
end tell
end tell
end try
end getNotificationTitles
Super excited for one of you to tell me about the infinitely easier way to get the same result!