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.
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).
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:
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.
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.