Skip to content

Instantly share code, notes, and snippets.

@drewkerr
Last active April 6, 2026 15:08
Show Gist options
  • Select an option

  • Save drewkerr/0f2b61ce34e2b9e3ce0ec6a92ab05c18 to your computer and use it in GitHub Desktop.

Select an option

Save drewkerr/0f2b61ce34e2b9e3ce0ec6a92ab05c18 to your computer and use it in GitHub Desktop.
Read the current Focus mode on macOS Monterey (12.0+) using JavaScript for Automation (JXA)
const app = Application.currentApplication()
app.includeStandardAdditions = true
function getJSON(path) {
const fullPath = path.replace(/^~/, app.pathTo('home folder'))
const contents = app.read(fullPath)
return JSON.parse(contents)
}
function run() {
let focus = "No focus" // default
const assert = getJSON("~/Library/DoNotDisturb/DB/Assertions.json").data[0].storeAssertionRecords
const config = getJSON("~/Library/DoNotDisturb/DB/ModeConfigurations.json").data[0].modeConfigurations
if (assert) { // focus set manually
const modeid = assert[0].assertionDetails.assertionDetailsModeIdentifier
focus = config[modeid].mode.name
} else { // focus set by trigger
const date = new Date
const now = date.getHours() * 60 + date.getMinutes()
for (const modeid in config) {
const triggers = config[modeid].triggers.triggers[0]
if (triggers && triggers.enabledSetting == 2) {
const start = triggers.timePeriodStartTimeHour * 60 + triggers.timePeriodStartTimeMinute
const end = triggers.timePeriodEndTimeHour * 60 + triggers.timePeriodEndTimeMinute
if (start < end) {
if (now >= start && now < end) {
focus = config[modeid].mode.name
}
} else if (start > end) { // includes midnight
if (now >= start || now < end) {
focus = config[modeid].mode.name
}
}
}
}
}
return focus
}
@nbonfire
Copy link
Copy Markdown

nbonfire commented Apr 6, 2026

ive been using jq with this bash script:

#!/bin/bash
# Get current macOS Focus mode from Do Not Disturb database

jq -s -r '
  # 1) Grab the latest-mode ID (or null if none)
  (.[0].data[0].storeAssertionRecords? // []
    | max_by(.assertionStartDateTimestamp)?
    | .assertionDetails.assertionDetailsModeIdentifier
  ) as $activeID

  # 2) If we got an ID, look it up; otherwise print "Free"
  | if $activeID then
      (.[1].data[0].modeConfigurations? // [])[]
      | select(.mode.modeIdentifier == $activeID)
      | .mode.name
    else
      "Free"
    end
' \
  ~/Library/DoNotDisturb/DB/Assertions.json \
  ~/Library/DoNotDisturb/DB/ModeConfigurations.json

(also needs full disk access, im using sequioa and it does show up in the full disk access gui)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment