Skip to content

Instantly share code, notes, and snippets.

@NightMachinery
Forked from drewkerr/set-focus-mode.js
Created November 26, 2022 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NightMachinery/698251e12d4545e532ae3e9e3b0b2690 to your computer and use it in GitHub Desktop.
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)
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