Skip to content

Instantly share code, notes, and snippets.

@drewkerr
Created April 13, 2022 14:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save drewkerr/c383ec799b05ac2db4121f5f2dfebd0a to your computer and use it in GitHub Desktop.
Save drewkerr/c383ec799b05ac2db4121f5f2dfebd0a to your computer and use it in GitHub Desktop.
Set a Focus mode on macOS Monterey (12.0+) using JavaScript for Automation (JXA)
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