Get current focus mode via script

What’s especially nice about @akerr’s script is that it teaches us how to get the focus mode using any language. The code is very clear.

2 Likes

@akerr 's script is super useful. If it is helpful to anyone, the solution I’ve been using is to just have a value in data jar that I update using shortcuts automation any time the focus mode changes. Then I can do automation with that.

1 Like

Here’s another handy tip that I just learned today:

defaults read com.apple.controlcenter "NSStatusItem Visible FocusModes"

If that returns 0 then there is no focus mode on.

If that returns 1 then there is some focus mode on.

Still need @akerr’s solution if you want to know which Focus Mode is on, but if you are just looking to turn something on or off, knowing the current ‘general’ state might be sufficient.

This is also useful for telling of your attempt at turning a focus mode on was successful or not. Check the defaults read after, and make sure that it says 1 and you know that something turned on at least.

I’d still love to find a simple shell-script friendly way to get the name of the Focus Mode, but for the time being I just made a wrapper script around the JXA solution.

1 Like

Any chance to set the focus mode via script?

Welcome! Probably easiest by creating a Shortcut with the Set Focus action and running with shortcuts.

Thank you akerr! This is great…

1 Like

Hi! First post here…just made an account to thank @akerr and all the others pitching in. Here’s the icloud link to the Shortcut I made with your help:

iCloud Shortcut: Toggle Focus Mode

It checks whether there’s a Focus Mode running. If there is, it turns focus off. If it’s off, it prompts you to select which Mode you want to turn on.

I finally used this in BetterTouchTool on a button for the TouchBar.

2 Likes

OH THIS IS PERFECT TIMING!

I just made a Stream Deck / Keyboard Maestro button for showing what is the current active Focus Mode, if any.

The next step was going to be how to do exactly what this Shortcut does.

Once I have a chance to test it a bit more, I will release it for the community to use.

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.