Skip to content

Instantly share code, notes, and snippets.

@arvindsv
Last active December 28, 2023 03:43
Show Gist options
  • Save arvindsv/ddc7d94659ea59b21ac5f8d2942c655d to your computer and use it in GitHub Desktop.
Save arvindsv/ddc7d94659ea59b21ac5f8d2942c655d to your computer and use it in GitHub Desktop.
Get and manipulate Apple macOS Focus mode (https://support.apple.com/en-gb/guide/mac-help/mchl613dc43f/mac)

Example:

$ chmod +x focus_mode

$ ./focus_mode
Usage:
  /usr/bin/osascript -l JavaScript ./focus_mode show
    Show the current macOS Focus mode. Outputs "null" if no Focus mode is currently active.

  /usr/bin/osascript -l JavaScript ./focus_mode toggle focus_mode
    Toggle the provided macOS Focus mode. "Do Not Disturb" is a standard focus mode, for instance.

$ ./focus_mode toggle 'Do Not Disturb'

$ ./focus_mode show
Do Not Disturb

$ ./focus_mode toggle 'Do Not Disturb'

$ ./focus_mode show
null
#!/usr/bin/osascript -l JavaScript
/* Get and change macOS Focus modes (https://support.apple.com/en-gb/guide/mac-help/mchl613dc43f/mac) */
function getArgs() {
var args = $.NSProcessInfo.processInfo.arguments
var argv = []
var argc = args.count
for (var i = 0; i < argc; i++) {
argv.push(ObjC.unwrap(args.objectAtIndex(i)))
}
delete args
return argv;
}
function menuItemByName(menuItemName) {
return controlCenterProcess.menuBars.at(0).menuBarItems.byName(menuItemName)
}
function checkboxByName(checkboxName) {
return controlCenterProcess.windows.byName("Control Centre").checkboxes.byName(checkboxName)
}
// This should cause the "Focus Mode" icon (the one with the crescent moon) to show up on the Menu Bar.
// It doesn't seem to show till you go into one of the Focus modes once. And it goes away by itself sometimes.
function enableFocusModeIconIfNeeded() {
if (menuItemByName("Focus").exists()) {
return
}
menuItemByName("Control Centre").click()
checkboxByName("Focus").click()
checkboxByName("Do Not Disturb").click()
menuItemByName("Control Centre").click()
}
function nameOfCurrentFocusMode() {
if (!menuItemByName("Focus").exists()) {
return null
}
enableFocusModeIconIfNeeded()
menuItemByName("Focus").click()
checkboxOfCurrentFocusMode = controlCenterProcess.windows.byName("Control Centre").checkboxes().find((checkbox) => checkbox.value() == 1)
nameOfCheckbox = (typeof(checkboxOfCurrentFocusMode) !== 'undefined' && checkboxOfCurrentFocusMode.exists() ? checkboxOfCurrentFocusMode.title() : null)
menuItemByName("Focus").click()
return nameOfCheckbox
}
function toggle(focusModeToToggle) {
enableFocusModeIconIfNeeded()
menuItemByName("Focus").click()
checkboxByName(focusModeToToggle).click()
menuItemByName("Focus").click()
}
var controlCenterProcess = Application("System Events").applicationProcesses.byName("ControlCenter")
var args = getArgs()
if (args.length > 1 && args[args.length - 1] == 'show') {
nameOfCurrentFocusMode()
} else if (args.length > 2 && args[args.length - 2] == 'toggle') {
toggle(args[args.length - 1])
} else {
console.log('Usage: ');
console.log(` ${args.join(' ')} show`)
console.log(' Show the current macOS Focus mode. Outputs "null" if no Focus mode is currently active.\n')
console.log(` ${args.join(' ')} toggle focus_mode`)
console.log(' Toggle the provided macOS Focus mode. "Do Not Disturb" is a standard focus mode, for instance.')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment