FEATURE REQUEST: New Trigger–Desktop Space Change

Hi @peternlewis. Please consider adding a Keyboard Maestro trigger for Desktop Space changes.

If I'm correctly reading the following entry in stack overflow, it appears there is a programatic method:

objective c - Detecting when a space changes in Spaces in Mac OS X - Stack Overflow

If you were to add that trigger, I'd be able to simplify and improve several macros that I use including a set that I've shared on the forum: Desktop Spaces • Macros to Improve Navigation and Window Management

Absent that trigger, in macros that need to save the Desktop Space, I've used the focused window changes trigger to query and save the space number. That trigger does not always change when the Desktop Space changes and obviously triggers many times when the space does not change.

On a related note, the tiling window manager yabai does detect Desktop Space changes. I've configured yabai to run a Keyboard Maestro macro that queries and saves the Desktop Space number to a Keyboard Maestro dictionary value. It's ugly, but it does work reliably.

Thanks for considering my request.

2 Likes

I will take a look.

1 Like

Done for the next version.

8 Likes

Wow, 13 hours; this has to be in the running for the shortest time between a feature request and done for the next version response. :hugs:

Thanks, @peternlewis!!

3 Likes

Well, when you've done the research and have pointers to how to do it, it definitely helps. Often such things cannot be done (most of the Spaces API is still private unfortunately).

4 Likes

Yabai requires disabling System Integrity Protection (SIP). The Github utility WhichSpace (I know you know this, Jim) does not notify, but it does track which Desktop Space is active and can report that via AppleScript.

tell application ¬
	"System Events" to tell process ¬
	"WhichSpace" to set newWhichSpaceNum to (title of menu bar items of menu bar 1)

return newWhichSpaceNum

Also see this thread and its links for more info on WhichSpace:

Yes, that's why I requested the new Space Changed Trigger and am delighted that @peternlewis included it with Keyboard Maestro v11. :grinning:

Soon I'll be updating these macros: Desktop Spaces • Macros to Improve Navigation and Window Management


Incidentally, this is the WhichSpace AppleScript that works most reliably:

tell application "System Events"
	tell process "WhichSpace"
		set DesktopSpaceList to (title of menu bar items of menu bar 1)
	end tell
end tell
return item 1 of DesktopSpaceList

If you run the AppleScript in Script Debugger you'll see the rationale for item 1 of DesktopSpaceList.

I tried doing that and I see DesktopSpaceList described as "list of 1 item". Is that what you mean, it's a list and needs to be dissected as such?

1 Like

Yes. If my memory serves me correctly, originally didn’t do that either but another user reported an issue and suggested the modification to the AppleScript.

I could never duplicate the issue but I went ahead and made the change since it’s technically correct.

I just recently saw that. In that instance, WhichSpace returned a list of 3 items, for some reason.

1 Like