1#!/usr/bin/env osascript -l JavaScript
2// Returns the current Focus mode on macOS.
4// Written by Drew Kerr:
5// https://gist.github.com/drewkerr/0f2b61ce34e2b9e3ce0ec6a92ab05c18
7const app = Application.currentApplication()
8app.includeStandardAdditions = true
10function getJSON(path) {
11 const fullPath = path.replace(/^~/, app.pathTo('home folder'))
12 const contents = app.read(fullPath)
13 return JSON.parse(contents)
17 let focus = "No focus" // default
18 const assert = getJSON("~/Library/DoNotDisturb/DB/Assertions.json").data[0].storeAssertionRecords
19 const config = getJSON("~/Library/DoNotDisturb/DB/ModeConfigurations.json").data[0].modeConfigurations
21 if (assert) { // focus set manually
23 const modeid = assert[0].assertionDetails.assertionDetailsModeIdentifier
24 focus = config[modeid].mode.name
26 } else { // focus set by trigger
29 const now = date.getHours() * 60 + date.getMinutes()
31 for (const modeid in config) {
33 const triggers = config[modeid].triggers.triggers[0]
34 if (triggers && triggers.enabledSetting == 2) {
36 const start = triggers.timePeriodStartTimeHour * 60 + triggers.timePeriodStartTimeMinute
37 const end = triggers.timePeriodEndTimeHour * 60 + triggers.timePeriodEndTimeMinute
39 if (now >= start && now < end) {
40 focus = config[modeid].mode.name
42 } else if (start > end) { // includes midnight
43 if (now >= start || now < end) {
44 focus = config[modeid].mode.name