MACROS: Desktop Spaces • Macros to Improve Navigation and Window Management, v1.1


Introduction

Mission Control Desktop Spaces, a.k.a. Desktop Spaces, a.k.a. macOS virtual Desktops, can be very useful particularly when using a MacBook. Unfortunately, however, configuration and use of Desktop Spaces can be confusing especially when using three or more. The current macOS maximum is 16.

In addition, native macOS does not provide:

  • A visual indication of the Desktop number. Moreover, macOS does not include a public API to determine the Desktop number or to switch Desktop Spaces.

  • A quick method to switch between non-adjacent Desktops.

  • An efficient method to move windows to non-adjacent Desktops

The instruction and macros in this post will address these shortcomings and more.


Requirements

This set of macro has two requirements:

  1. Keyboard Maestro v10+ (because macro subroutines are used)

  2. WhichSpace (a free, third-party menubar application)


Differences/Improvements Versus Superseded Macros

This set of macros supersedes all macros found in forum thread Desktop Spaces • Macros for Navigation and Window Management.

Expand for details
  • The macros will be installed in macro group: .sys Desktop Spaces. The group can be renamed, but it is recommended that the macro be kept together.

  • The group consists of 57 macros:

    8 Primary Macros—4 for navigation and 4 for window movement:

    • Go Left a Desktop
    • Go Right a Desktop
    • Go to Application Desktop
    • Go to Previous Desktop
    • Move Application Window to Current Desktop
    • Move Window Left a Desktop
    • Move Window Right a Desktop
    • Move Window to Previous Desktop

    7 Subroutine Macros:

    • sub—Activate an App and Wait Until It Is Ready
    • sub—Get DesktopNo
    • sub—Get DesktopNo_prev
    • sub—Go to Desktop N
    • sub—Move Window to Desktop N
    • sub—Put DesktopNo and/or DesktopNo_prev
    • sub—Type Keystroke for Switch to Desktop N

    42 Conflict Palette Macros

  • Due to the modular design:

    • The macros have been simplified.

    • The 8 primary macros and 7 subroutine macros can easily be used by other macros in the Keyboard Maestro library. Also, macro hot keys and underlying macOS keyboard shortcuts can now be easily changed (if the default configuration causes collisions with existing library macro hot keys and/or macOS keyboard shortcuts).


Disabling Previous Versions

If you previously installed any macros from Keyboard Maestro thread Desktop Spaces • Macros for Navigation and Window Management, complete the following:

  1. If it exists, disable the .sys Spaces macro group. Rename it .sys Spaces-superseded.

  2. If it exists, disable the .sys Desktops macro group. Rename it .sys Desktops-superseded.

Afer completing the installation steps below, optionally come back and delete these two disabled macro groups and all macros within.


Version History

v1.1 (2022-04-06)

  1. macro: Go to Previous Desktop

    Bug fix: local_dnPreviouslySaved was improperly determined.

  2. macro: Move Window to Previous Desktop

    Removed an extraneous local_dnTarget Set Variable to Text action. No change to macro functionality.

  3. macro: sub—Activate an App and Wait Until It Is Ready

    Added provision for applications that have a null %FrontWindowName%.

v1.0 (2022-03-21) Initial version


Installation

To install Desktop Spaces • Macros to Improve Navigation and Window Management, complete the following:

  1. If necessary, using Mission Control, create multiple macOS Desktop Spaces.

  2. Configure System Preferences > Mission Control.

  3. Configure System Preferences > Keyboard > Shortcuts > Mission Control.

    Switch to Desktop 1 .. 9		⌃⌥⌘1 .. ⌃⌥⌘9
    Switch to Desktop 10 .. 16		⌃⌥⇧⌘0 .. ⌃⌥⇧⌘6
    

  4. Click to download the following:

    Desktop Spaces • Macros to Improve Navigation and Window Management.zip (101.2 KB)

  5. Unzip the download and follow the instructions included in the enclosed _READ ME FIRST_ files.


Macro Group Image

2 Likes

One of the moderators here always says, if you try to figure something out for an hour and can't, come ask for help, so that's what I'll do. For the life of me, I can't get this macro to work for me. I did not have the prior version installed, so that is not the issue. The macro group is enabled (see below), so that is not the issue. I have made the changes in SysPrefs as shown - indeed, I can use ctr-opt-cmd-1 to switch to first desktop, ctr-opt-cmd-2 to the second, etc, and ctr-left/right arrow to move forward or backward a space, whether the macro group is enabled or not, so that is not the issue.
The macro works to the following extent: Ctrl-d brings up the conflict palette as expected, but selecting a menu option appears to do nothing, OTHER THAN when I try to move a window from say Desktop 1 to Desktop 2, I can see the cursor move up near the "traffic lights" as expected, but after that nada. The "go to Desktop" macros don't work, whether I try to run them from the conflict palette, or directly by clicking on the Run button in the KM editor.
I have pored through each of the macros and subroutines and can't see anything that would be contra the specifics of my set-up -- in fact, I am wanting to use this macro because I got a new Mac Studio, set up from scratch, and am working on a single monitor until my Studio Display shows up in a couple of weeks. I've been a two-monitor guy for ages, but was hoping that maybe this macro group would let me work just with the Studio Display and no second monitor.
Any thoughts?

PS
Thanks for your many contributions - I am running other macros you have created with no problem and they are terrific.

1 Like

Hi @rolian, sorry you are have difficulty.

Assuming you are using Keyboard Maestro v10+, let's start the troubleshooting with this:

  1. In the same group .sys Desktop Spaces, create a new macro named zzz (so it appears at the end of the macro list).

  2. Add this action to the new zzz macro:

Type a Keystroke.kmactions (597 B)

Keyboard Maestro Export

  1. Manually navigate to Desktop Space 2.

  2. Trigger the new zzz macro.

That should cause your display to switch to Desktop Space 1.

Either way, please let me know what you observe.

Nope, that doesn’t work - I’d tried that before in a slightly different way, but did it again importing your action as you suggested just to double check, but still no luck. What is odd is that just typing the ctr-opt-cmd_# keystrokes works as expected from the SysPref assignments—i.e. the SysPRef assignments all work, but once they are put to use in KM, they don’t work in the macros. Even just clicking “Try action” on the gear menu of your action doesn’t cause the desktop to switch. One thing I did notice, though I’m not sure it is relevant, is that ctr-opt-cmd-1 through -7 are assigned keys in Finder (in the View/Sort menu) and when I’m in Finder, those key assignments seem to trump the assignments made in SysPrefs. I will pursue this tomorrow with wholly different hotkey assignments for the Desktops and see if that changes anything, but I’m not sure why no one else wouldn’t be seeing the same issue if Finder is the problem and, as noted, the Desktop switch hotkeys work when pressed outside the KM environment.

I’ve been using KM a long time and can’t fathom how/why this would be the case. Clearly it is not anything in the logic of your macro, so I’m not sure there’s any more I can ask you to do. But if you (or any others) have any ideas, I’m all ears...

1 Like

@rolian, thanks for reporting back with such comprehensive information. That's a big step in diagnosis because it completely bypasses the logic in the macros I've provided in this thread.

You can jump straight to the the thread I've included below, but it would likely be helpful to first try Type a Keystroke two additional ways using default macOS System Preferences keyboard shortcut assignments:

1. Turn Dock Hiding On/Off

Preference and Keyboard Shortcut

For...

Does this function as expected?

Keyboard Maestro Export


2. Show Help Menu

Preference and Keyboard Shortcut

For...

Does this function as expected?

Keyboard Maestro Export


If either of the above does not function as expected, then I suspect you have a problem that could be addressed using the information included in this thread: Simulate Keystroke Not Working in Mojave

The Show Help Menu one works fine, whether within or outside of KM. The Dock Hiding one doesn't work in either place (which may be because of some preference I set somewhere). I did go through the link you referenced, turned Accessibility off for both the the Editor and the Engine, rebooted and then turned them both on, but still can't get the desktop switching macros to work (though the desktop switches work if I just invoke them in whatever app I'm in0.
Thanks for these additional thoughts and assistance.

1 Like

Should have tried debugger a couple of hours ago!! Your macro set is cancelling out for me at the step where WhichSpace is supposed to be returning the desktop number. I do have WhichSpace running (ver 0.3.2) - I can see the icon in my menu bar, and it doesn't look like it has any settings, so I don't think I could have screwed up anything there - so am not sure what might be going wrong.

Specifically, this is what is not working:

It seems to get stuck here for a long time- it returned 1 even when I was on Desktop 2 or 3 (I changed it to Save to Clipboard for testing purposes just now - all previous runs were based on saving it the variable as you had it), but now several minutes later, the clipboard is returning 3 (the desktop I was on when I last ran the action.
Ok, one last data point - when I run the WhichSpace script in Script Editor, it is relatively speedy ( < 2 secs) and correct

1 Like

How about the original test with macro zzz? Did that start working after the steps you summarized above? If so, then it seems that one problem may have been solved.

Also, did the Show Help Menu test work before or after the Accessibility steps

Please try this test macro:

DOWNLOAD Macro File:
zGetDesktopNo Test.kmmacros (13 KB)
Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.

Macro-image


This is what I see on my system:

I got this on the first run:

CleanShot 2022-03-28 at 10.40.29

and this on a second run, after switching desktops:

CleanShot 2022-03-28 at 10.42.10

As an aside, I am using a brand new Mac Studio Max, so it shouldn't be my processor that's holding things back! And, in case it matters, I installed WhichSpace via Homebrew.

Unfortunately I don't have access to a Mac with the Apple Silicon processor, so it's going to be difficult for me to help with this particular issue. However, I see this: Add support for Apple Silicon · Issue #28 · gechr/WhichSpace · GitHub

So, from Terminal, you might try this:

arch -arm64e brew install --cask whichspace

BTW, you may have seen other threads in the forum that have identified AppleScript issues with Monterey 12.3. It's a WAG, but maybe the issue you are seeing is related.

Thanks. I'll try that and let you know a little later -have to hop on a call. After that, I will just try to suss this out on my own - I think we've established that your macro set is not the issue and so it isn't fair to ask you to keep spending time on this - instead, you can go create some more amazing macros!

1 Like

Well, the ARM64 version generated times of 18 and 22 seconds when running zGetDesktopNo Test.kmmacros , so no help there. I will pursue your idea of going back to review the AS/12.3 threads, but unless I get lucky, I guess I am back to using the system shortcuts to switch desktops and giving up on the ability to move windows from one desktop to another. Or maybe I'll just bite the bullet and get a second monitor again and forget about desktops altogether LOL.

Thanks again for all of your help and sticking with me on this for as long as you did (and for creating the macros for the community in the first place).

1 Like

No problem; I enjoy helping others with challenges like this.

Only because you said that the AppleScript seemed to work better from the Script Editor, I think it might be worth testing this slightly revised approach:

DOWNLOAD Macro File:
zGetDesktopNo Test v2.kmmacros (13 KB)
Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.

Macro-image


Also, even if this doesn't work, I think this would be worth revisiting once Monterey is revised beyond v12.3.

Curiouser and curiouser...

Run #1 had me excited:
local_DesktopNo [3]

START Time: 70592.852918
END Time: 70593.10928
DURATION: 0.256362 sec

But then:
local_DesktopNo [2]

START Time: 70617.105974
END Time: 70619.966049
DURATION: 2.860075 sec

And then back to Desktop 3 to see if there was something special about that, but no:

local_DesktopNo [3]

START Time: 70722.791659
END Time: 70731.557042
DURATION: 8.765383 sec

I sure wish I had an Apple Silicon machine so I could do more testing, but short of that, here's another revision to try. It's a bit slower on my Intel Mac, but hoping it might yield more consistent results on your Studio.

DOWNLOAD Macro File:
zGetDesktopNo Test v3.kmmacros (13 KB)
Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.

Macro-image

Pretty much the same pattern. Good result on Desktop 3 first time around, but not the second time, so seemingly not desktop-specific. In the order I ran them:

local_DesktopNo [2]

START Time: 82849.869349
END Time: 82865.350882
DURATION: 15.481533 sec


local_DesktopNo [3]

START Time: 82884.58118
END Time: 82885.333274
DURATION: 0.752094 sec


local_DesktopNo [1]

START Time: 82901.161815
END Time: 82905.271383
DURATION: 4.109568 sec


local_DesktopNo [3]

START Time: 82919.705455
END Time: 82925.204339
DURATION: 5.498884 sec

Sorry to hear that. Let's revisit this post Monterey v12.3.

Hi @rolian,

Looks like at least one other AppleScript issue has been addressed with macOS12.3.1. If you’ve updated, it might be worth checking the behavior of the WhichSpace AppleScript.

Hi @_jims,

I updated to 12.3.1 but don't see any real difference. The macro group still doesn't work for me, and the three test macros you'd sent still yield times in the 4 to 10 second range. I have an idea though - I'm going to see if I can carve off just the part of your macro that puts and holds the cursor by the "stoplights" and then use that as the first action in a StreamDeck multi-action. The second action will be to use the system hot-key for switching to a desktop (e.g. ctr-opt-comd-{# of desktop}. May not get to try this until tomorrow, but will certainly keep you posted.