-
-
Save NightMachinery/698251e12d4545e532ae3e9e3b0b2690 to your computer and use it in GitHub Desktop.
Set a Focus mode on macOS Monterey (12.0+) using JavaScript for Automation (JXA)
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
function toggleFocus(focus) { | |
const app = Application("System Preferences") | |
const pane = app.panes.byId("com.apple.preference.notifications").anchors.byName("Focus") | |
app.reveal(pane) // Open the preference pane | |
// Useful way of inspecting the UI hierarchy of an open app: | |
// Application("System Events").applicationProcesses.byName("System Preferences").entireContents() | |
const ui = Application("System Events").applicationProcesses.byName("System Preferences").windows.byName("Notifications & Focus").tabGroups.at(0) | |
delay(0.5) // Ensure the UI has loaded | |
const rows = ui.scrollAreas.at(0).tables.at(0).rows() | |
rows.forEach(row => { | |
let name = row.uiElements.at(0).staticTexts.name() | |
if (name == focus) { | |
row.select() // Select the focus mode, if found... | |
ui.groups.at(0).checkboxes.at(0).click() // ... and toggle the switch | |
} | |
}) | |
app.quit() | |
} | |
toggleFocus("Do Not Disturb") // Or whichever you like |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment