1

I am an avid user of the Mission Control Desktop Workspaces feature of Mac OS. But keeping track of which space I'm in, and even moreso what the previous space was, is frequently a problem.

There are some little tools on GitHub such as WhichSpace (currently at v0.3.2) which do things like put a little icon in the tool bar to tell you the number of the Desktop Workspace you're currently in. But it would be hugely more useful if a change of workspace could trigger an AppleScript script.

There are many various ways that the active desktop workspace can be changed manually. I can use the keyboard hotkeys Ctl+Left and Ctl+Right, swipe left or right, open Mission Control and pick a Desktop from the list that appears at the top of the screen, etc. And there are ways the desktop workspace gets changed without my direct decision. I click an application in the taskbar and instead of opening a new window, I get taken to the space that already has an open window. I use the new Chrome tab list (the down arrow at the upper right) and pick a tab and I have no idea what space that window is in, but I go there.

Is there some way in which, no matter what is causing me to change spaces, in or out of my control, a script or function could be called and be given parameters of the old and new space numbers, or at least the new space number?

I am looking for ways to replicate some of the functionality of CurrentKey Stats, which is no longer supported. In the latest versions of CKS, a feature can be turned on so that whenever the space was changed, by whatever means, an AppleScript script is called:

/Users/[username]/Library/Application Scripts/com.currentkey.stats/ck.scpt

and a function in that script is given a parameter of the identity of the new space. This turns out to be really useful.

That's what I'm trying to do, in a way that is not dependent on abandoned software. But doing anything along those lines seems to hinge on being able to be notified whenever any app or system process changes the current space and what that change is.

Does anyone know how to do that?

(The author of CurrentKey just became a father last week, so I don't expect to hear from him anytime soon, and in previous correspondence, he said he couldn't recommend his method, whatever it was.)

6
  • 1
    I think you'll struggle to automate this, Spaces is notoriously unscriptable. Some pointers perhaps… "workspace gets changed" - try apple.stackexchange.com/a/214349/85275 For some general Spaces setup info, try superuser.com/a/1187552/347380 and apple.stackexchange.com/a/179403/85275 Also note that fullscreen Spaces aren't numbered, so they cannot be accessed directly, nor can their order be permanently set.
    – Tetsujin
    Mar 15, 2022 at 9:26
  • Is it fine for you to create something with Swift? Because you can use NSWorkspace notification + some private API to get SpaceID. With that you can launch your script. Note SpaceIDs might be "out of order" if you've moved them around, so I'm not sure if that number is helpful. Mine are for example (from leftmost) 3,5,2,12,13 Apr 6, 2022 at 7:49
  • Thanks. I have never used Swift and your idea, "+ some private API" I don't have any idea where to start. I can program in Bash, JavaScript, TeX, VIM macros, a little Ruby and Python (let's not forget Troff), but I need handholding at the OS level. Also, when I change the order of desktops/workspaces in Mission Control by dragging, the numbers automatically change.
    – August
    Apr 6, 2022 at 8:33
  • @MateuszSzlosek -- You are not the first person that I have seen describe this behavior, but for me, forever, when I rearrange the order of the desktops, the numbering changes. For example, I have been jumping back and forth between Desktop 8 and Desktop 19 while testing some code, so I just now moved Desktop 8 to be to the left of Desktop 19. Everything renumbered and what was Desktop 8 is now Desktop 18 and they all display in numerical ordere.
    – August
    Aug 25, 2022 at 5:38
  • @AugustMohr by SpaceID I mean the number you get from private API, not the displayed number in MissionControl. Here's sample app that prints SpaceID github.com/slozo/NSWorkspaceNotification Aug 25, 2022 at 7:33

0

You must log in to answer this question.

Browse other questions tagged .