Skip to main content

Add a script to get focus mode

ID
b3d51e4
date
2022-11-18 16:25:27+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
d11b1c4
message
Add a script to get focus mode

Co-authored-by: Andrew Kerr <drewkerr@users.noreply.github.com>
changed files
2 files, 52 additions

Changed files

.gitattributes (82) → .gitattributes (126)

diff --git a/.gitattributes b/.gitattributes
index 50dbde9..6ca6507 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1,2 +1,3 @@
 close_tabs linguist-language=JavaScript
+get_focus_mode linguist-language=JavaScript
 set_accent_colour linguist-language=Swift

get_focus_mode (0) → get_focus_mode (1538)

diff --git a/get_focus_mode b/get_focus_mode
new file mode 100644
index 0000000..3e72983
--- /dev/null
+++ b/get_focus_mode
@@ -0,0 +1,51 @@
+#!/usr/bin/env osascript -l JavaScript
+// Returns the current Focus mode on macOS.
+//
+// Written by Drew Kerr:
+// https://gist.github.com/drewkerr/0f2b61ce34e2b9e3ce0ec6a92ab05c18
+
+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
+}
\ No newline at end of file