This is a Macro that I wrote to control HomeKit with Siri using KM or other Scripts. Currently there is no direct way to control HomeKit with scripting (Mojave did introduce a Siri SDK "Sirikit", that Peter is now aware of, see: Can Siri Be Used to Trigger a KM Macro?).
This Macro utilizes Mac OS Mojave's "Type To Siri" feature in which you can activate a Siri prompt and type in text to ask Siri for information or for controlling HomeKit devices.
First you must enable "Type To Siri" in System Preferences / Accessibility / Siri / "Enable Type To Siri"
NOTE: The default "Type To Siri" activation is "Hold Command Space", my macro uses "Hold Option Space", but you can change this to what you like in System Preferences, but be sure to then also change the Keystroke Action in the Macro.
The Macro runs some UI Scripts via AppleScript - which can sometimes be unreliable. I've tried to make them as reliable as possible while also adding some simple error notifications if something doesn't work. Problems may occur if you're running this on a Mac that's receiving lots of Notifications at the same time that the macro is run. As long as the "Type To Siri" notification window is the 'last' notification window shown, then it should work fine. I could fix this so that window order doesn't matter, but it wasn't necessary for my purposes as I have this running on a headless Mac Mini that's not receiving other notifications.
These are just some of the many things I execute with this macro.
I'm typically running remote KM Macros (from other Macs) that trigger this "Type To Siri" Macro on my Mac Mini. For instance, when I return to my Mac Pro after lunch and I move the mouse, the Lights and Speakers turn back on (if they were on previously).
I also have a Hard Drive Dock that I use for Backups. When it's time to run the backup, I have a KM macro use Type To Siri to turn on the Wifi / HomeKit switch that the Dock is connected to, and then the backup runs. When the backup finishes another KM Macro runs and checks to make sure the Backup Volume has been unmounted and then runs another Type To Siri command to turn the HomeKit switch off, thus turning the Dock off.
Set the TypeToSiriQuery variable to a Siri query like "Turn Off Studio Light" etc.
Improved AppleScript code to 'find' the correct Notification Window for Type to Siri instead of having the window 'set' to 'first' or 'last' etc. This should make the macro a bit more reliable when there happens to be multiple notifications on screen.
As a result of 1. above, I have removed the 'repeat this action 2 times' because it should not be necessary any longer.
Wow. Just wow. Did I say wow yet? That's 20 times more impressive than my primitive method for addressing HomeKit control. Your post is informative and innovative. Brilliant. AppleScript always makes me struggle so I'm really impressed. Very inspiring. I will study this. That's a whole different approach that I should have come up with myself... but didn't. Throw my old ideas away. I wonder what else can be done with this approach. I haven't even loaded your macro yet and I can see it's a winner. But it would sure be nice if the next release of KM could support this a little more directly with an action. After I study your solution I may propose a new action. But I hear it's on Peter's future feature list, so there's no point in pontificating.
Thanks Peter, that's an interesting idea. Is there any other benefit besides the fact that I'll be able to remove the "Set Variable" action? I suppose I'll actually be moving & modifying the "Set Variable" action into the Execute Macro Action.
All - I must be missing something or this setup process is missing a step. When creating the macro as shown above the "Type To Siri" selection does not appear in the dropdown menu of "All Macros" to choose from when selecting the "Execute Macro" step.
In the attached screenshot you can see that there is no "Type To Siri" macro. Did I somehow miss a step to create a macro called "Type To Siri" first? If that macro does not exist the subsequent steps cannot be followed.
I get the rest of it and it all looks logical to me but I can't build this unless I can get past this step. Can someone assist?
Thank you. I did miss that. My apologies for not seeing that at the end of the post. I’m very familiar with the use of AppleScript and was a former Quickeys user (remember that app?). Using forums for help is unusual for me and I simply overlooked the downloadable macro.
I appreciate the help from the community and the quick response. Now it’s back to HomeKit and KM. I’m going to attempt to connect my iMac to my desk lamp (plugged into a Wemo smart switch and connected to my HomeKit through a HomePod as a hub) so that when the screensaver comes on (or the computer locks) it issues a command to Siri for it to “turn off the desk lamp.”
Thanks to @rolian and any others that may have read my post.
Does anyone have this macro working with Big Sur? It appears the AppleScript for interacting with Siri has changed and I'm not sure how to figure out what the equivalent elements should be. Any help is appreciated!
Here's an updated version of the AppleScript that will run on Big Sur:
tell application "System Events" to tell the front menu bar of process "SystemUIServer"
tell (first menu bar item whose description is "Siri")
perform action "AXPress"
tell application "System Events" to tell the process "NotificationCenter"
set notifyWindows to every window
repeat with notifyWindow in notifyWindows
if description of notifyWindow is "system dialog" then
set elements to entire contents of notifyWindow
tell (first text field of first group of first UI element of first scroll area of notifyWindow)
set focused to true
set value to ASTypeToSiriQuery
perform action "AXConfirm"
Edit: I should mention, I'm not very good with AppleScript, so this is probably going to break in the future, but it works for now.