Get current focus mode via script

New to this community, and TIL there’s a command line to run Shortcuts. That’s most excellent! I use ToothFairy to quickly connect to my AirPods—which I pretty much only have in my ears if I’m in a Zoom meeting—and now I can automatically set my Focus mode to Work when my AirPods connect.

Thank you, @akerr!

1 Like

Glad it’s helpful! Welcome and thanks for your contribution.

If there’s a way to programatically set a focus mode by name WITHOUT using Shortcuts, I’d love to hear about it. I want to make switching focus modes via Bunch a matter of just typing “focus Work” without making users have to generate an on and off shortcut for every focus…

Welcome Brett and thanks for all your great development work over the years!

I don’t think focus mode can be set in the same way I was getting it - writing to those json files doesn’t seem to trigger a change (I tried copying Assertions.json, setting a mode and writing back the original). Unless the system is more sophisticated, using timestamps and other metadata.

I think the easiest method would be to script the UI (gist):

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

(there might be permissions hassles for your intended usage?)

Get a list of the focus modes from ModeConfigurations.json to populate a menu.

I know the shortcuts way works, but I decided to script the UI as well. But, the one in the menu bar, rather than the preferences pane.

My solution is here: Get and manipulate Apple Mac OS X Focus mode (https://support.apple.com/en-gb/guide/mac-help/mchl613dc43f/mac) · GitHub

Works well enough for me. Thought to mention it here in case it interests anyone. Since it brings up the UI panel, it will be visible for a fraction of a second.

1 Like

:wave:t3: @akerr,

As it’s the first time I’ve really used Shortcuts on the Mac, I’m encouraged that this really is a drop-in that can be run “as-is.” Most of the other automation I’ve done is in the UNIX realm (Ruby, shell scripting, etc)

With that said, how are you typically running this? The Menu Bar seems like an appropriate place to me, but still curious to hear what others are doing with it.

Welcome @chrisfinazzo

I’m not using that particular script at the moment (I had more fun trying to work it out) but I tend to run shortcuts from the menu bar and other scripts via Alfred or keyboard shortcuts.

1 Like

Is this supposed to work on Ventura? I run the script and the only output is ‘null’ no matter if a focus mode is on or not.

It worked the last time I tested it but there is Shortcut support in Sonoma to Get Current Focus so it’s no longer needed.

Keep in mind that Sonoma is not in public release (only recently public beta), and that not all devices that can run the prior solution will necessarily support running Sonoma. In addition, many users will not update to Sonoma for a while to ensure stability, hardware compatibility, etc. - particularly where there is a dependency on stability and compatibility for livelihood.

As such, the pre-Sonoma solution would be relevant to a decreasingly significant number of users for some time yet.

1 Like

Now that Sonoma is at the RC2 stage and set for release next week, what’s the latest on triggering a shortcut when enabling a specific focus mode like “sleep”? I’m on Sonoma, and in the shortcuts app I do see “get current focus”. But I’m not sure how to use that as a dynamic trigger when focus modes change?

macOS still doesn’t have Shortcuts automations. You can trigger with keyboard shortcuts or the menu bar and there are plenty of system hooks (e.g. launchd) for running scripts at times, on file/network changes etc but it’s a bit deep for most users.

I haven’t tried it but Shortery might be the closest to what you are describing.