Last active
November 28, 2023 18:40
-
-
Save ov1d1u/8e6ec1f0a087d1f410c65f16217273ba to your computer and use it in GitHub Desktop.
Get MacOS mic status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import requests | |
import atomacos | |
sysui = atomacos.getAppRefByBundleId('com.apple.controlcenter') | |
hass_api_url = "https://hassio.ov1d1u.net/api/webhook/<webhook_id>" | |
state = { "incall": False } | |
while True: | |
elems = [elem for elem in sysui.findAllR() if 'AXDescription' in elem.getAttributes()] | |
descs = [elem for elem in elems if 'Microphone' in elem.AXDescription.split() and 'use' in elem.AXDescription.split()] | |
is_mic_on = bool(descs) | |
new_state = { "incall": is_mic_on } | |
if new_state != state: | |
try: | |
r = requests.post(hass_api_url, json=new_state) | |
print("response:", r.text) | |
except: | |
pass | |
state = new_state | |
time.sleep(0.16) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>Label</key> | |
<string>net.ov1d1u.pyhton.micstatus</string> | |
<key>ProgramArguments</key> | |
<array> | |
<string>/usr/bin/python3/python3</string> | |
<string>/Users/ovidiu/Projects/micstatus.py</string> | |
</array> | |
<key>StandardErrorPath</key> | |
<string>/tmp/python_script.error</string> | |
<key>KeepAlive</key> | |
<true/> | |
<key>WorkingDirectory</key> | |
<string>/tmp</string> | |
</dict> | |
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- trigger: | |
- platform: webhook | |
webhook_id: <webhook_id> | |
binary_sensor: | |
- name: "In Call" | |
unique_id: fccb4eb1-5dd2-4d83-a15b-0abff69612c6 | |
state: "{{ trigger.json.incall }}" |
This is very useful, thank you for this gist.
Has anybody seen a memory leak by running this for long periods (as intended)?
macOS 13.5.2 (22G91) on an Apple M1 iMac.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry, but I am new to running python3 as a daemon, and when running the script with launchctl, it isn't loading the modules. How do I correct that? It works great when run by the user.
I installed both modules using
python3 -m pip install
UPDATE:
I forgot to use the Homebrew path for python3. All is working fine now.
Thank you so much for posting this!