MACROS: Desktop Spaces • Macros to Improve Navigation and Window Management, v4.0

Disclaimer: The following screenshot is likely unintelligible prior to reading the text included in this post. If fact, it might not provide tangible benefit until one has installed and experimented with this Mission Control Desktop Spaces macro group. With that said, I suspect that new users will find it to be a helpful quick reference.


Keyboard Maestro Version 11+ Required

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.


Requirement and Recommendation

This macro group has one requirements and one recommendations:

  1. Keyboard Maestro Version 11+

  2. Optional, but strongly recommended: WhichSpace (a free, third-party menu bar application)


Differences/Improvements Versus Superseded Macros

This macro group 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 62 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

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


Deleting Previous Versions

Note: It is very important to delete older versions of these macros before completing the installation of Version 4.0.

If you previously installed any macros from the Keyboard Maestro forum threads Desktop Spaces • Macros for Navigation and Window Management or Desktop Spaces • Macros to Improve Navigation and Window Management, complete the following steps:

  1. For the .sys Spaces macro group, rename it .sys Spaces-superseded. For the renamed group export it, then delete it.

  2. For the .sys Desktops macro group, rename it .sys Desktops-superseded. For the renamed group export it, then delete it.

  3. For the .sys Desktop Spaces macro group, rename it .sys Desktop Spaces-superseded. For the renamed group export it, then delete it.

In the unlikely event that it becomes necessary to view the macros deleted above, the exported groups can be imported, but only after installing Version 4.0 below.


Version History

Expand for details

v4.0 (2024-10-21) • Requires Keyboard Maestro Version 11+

Summary of Improvements

macro: Go Right a Desktop

  • Modified the logic such that sub—Put DesktopNo and/or DesktopNo_prev is not run if there is an attempt to move beyond the rightmost desktop.

macro: Go to Previous Desktop

macro: Move Window to Previous Desktop

macro: sub—Go to Desktop N

macro: sub—Type Keystroke for Switch to Desktop N

  • In the Switch/Case action, changed the condition >= 16 back to >= 16.

  • Enabled Returning a value and return 0 if a local_DesktopNo is a value that is not configured.

macro: 20)Previous ⌃⌥↩

  • Renamed from 20)Previous ⌃↩

macro: 43)to Previous [^+click] ⌃⌥⌘↩ [2t]

  • Renamed from 43)to Previous [^+click] ⌃⌘↩ [2t]

v3.0 (2024-06-21) • Requires Keyboard Maestro Version 11+

Summary of Improvements

  • Macros Go Left a Desktop (& Move Window Left a Desktop) and Go Right a Desktop (& Move Window Right a Desktop) have been improved:

    • While holding the modifier (default Control ⌃), if the right arrow or left arrow are tapped, the display is moved directly that many Desktop Spaces. For example, if you are viewing Desktop 4 and press and hold Control and tap the right arrow three times, the display will be moved directly to Desktop 7.

    • In contrast, while holding the modifier, if the right or left arrows are held down, the display will be moved right (or left) until the arrow key is released. In this case, all Desktops between the starting point and the ending point will be briefly visible. This method is likely useful if you are searching for a particular window as you are moving through the Desktops.

    • Optionally, when moving a display and window, the display can be returned to the original Desktop Space. Two methods can be used to achieve this optional behavior:

      1. via hotkey: hold down the modifier keys and tap the hotkey integer twice.

      2. via the Desktop Spaces palette: when clicking the palette item, hold down the Control (⌃) key.

  • With some limitations, the macro set can now be used without WhichSpace . If it is not installed and running, the Desktop Space Number will not be visible in the macOS menubar. Also, tracking of the current and previous Desktop will be temporarily inaccurate if a Desktop change occurs outside the control of these macros. For example:

    • three-finger swipe left or right on a trackpad

    • Mission Control Desktop Spaces is displayed and a Desktop is selected

    • a macOS application is opened, or activated, and an automatic Desktop change occurs

  • The Desktop Space palette has been annotated to indicate items that can be optionally Control (⌃)+clicked and hotkeys that can be tapped twice [2t] or multiple times [mt].

  • Version 3.0 of this macro group includes other improvements detailed below.

macro: Go Left a Desktop

  • Completely rewritten to provide the features described above.

  • Added logic to allow this macro set to run without WhichSpace running, albeit with some limitations.

macro: Go Right a Desktop

  • Completely rewritten to provide the features described above.

  • Added logic to allow this macro set to run without WhichSpace running, albeit with some limitations.

macro: Go to Application Desktop

  • Added logic to allow this macro set to run without WhichSpace running, albeit with some limitations.

macro: Go to Previous Desktop

  • Added logic to allow this macro set to run without WhichSpace running, albeit with some limitations.

macro: Move Application Window to Current Desktop

macro: Move Window Left a Desktop

  • Completely rewritten to provide the features described above.

  • Added logic to allow this macro set to run without WhichSpace running, albeit with some limitations.

macro: Move Window Right a Desktop

  • Completely rewritten to provide the features described in the PURPOSE.

  • Added logic to allow this macro set to run without WhichSpace running, albeit with some limitations.

macro: Move Window to Previous Desktop

  • Added logic to optionally return to the original Desktop Space.

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

  • Deselected Returning a value since it was not being used.

macro: sub—Get DesktopNo

  • Added condition to set to -1 if no valid value is retrieved from the dictionary. This handles rare, but possible boundary conditions.

  • Added logic to allow this macro set to run without WhichSpace running, albeit with some limitations.

macro: sub—Get DesktopNo_prev

  • Added condition to set to -1 if no valid value is retrieved from the dictionary. This handles rare, but possible boundary conditions.

macro: sub—Go to Desktop N

  • Added logic to allow this macro set to run without WhichSpace running, albeit with some limitations.

  • Reduced execution time by removing a hard-coded pause.

  • Deselected Returning a value since it was not being used.

macro: sub—Move Window to Desktop N

  • Added logic to optionally return to the original Desktop Space.

  • Deselected Returning a value since it was not being used.

macro: sub—Put DesktopNo and/or DesktopNo_prev

  • Simplified the conditional logic since sub—Get DesktopNo and sub—Get DesktopNo_prev were modified to set the value to -1 if no valid value is found.

  • Deselected Returning a value since it was not being used.

macro: sub—Type Keystroke for Switch to Desktop N

  • In the Switch/Case action, changed the condition = 1 to <= 1.

  • In the Switch/Case action, changed the condition = 16 to >= 16.

  • Updated the header comment with updated instructions.

macro: 01)⎯⎯⎯⎯⎯ [ go to Desktop Space ] ⎯⎯⎯⎯⎯

  • The macro serves as the left header for the macro set palette. If this header is inadvertently clicked, the macro now properly causes the palette to reappear. From the user's standpoint, this entry now behaves as if it is static text. In Version 2.0, the palette would dismiss.

macro: 24)⎯⎯⎯⎯⎯⎯ [ move Window ] ⎯⎯⎯⎯⎯⎯

  • The macro serves as the right header for the macro set palette. If this header is inadvertently clicked, the macro now properly causes the palette to reappear. From the user's standpoint, this entry now behaves as if it is static text. In Version 2.0, the palette would dismiss.

Added palette macros:

  • 22)┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
  • 23)[ ] == optional; mt == multiple taps
  • 45)┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
  • 46)^+click or 2 taps of hot key returns to Space

v2.0 (2023-10-27) • Requires Keyboard Maestro Version 11+

Summary of Improvements

  • Forty-three of the fifty-eight macros in this macro group are included in a palette. This palette is displayed using one of the macros in this group, ➜ Desktop Spaces - Show Palette of Macros, that includes a Show Palette of Macros action. By default, the palette can be displayed using ⌃D. Keyboard Maestro Version 11 provides a new feature that allows such palette to have an individual style. This feature has been used to optimize the appearance of the palette for this macro group. Previously the much less flexible Conflict Palette was used.

  • The macro Go to Previous Desktop uses a new Keyboard Maestro Version 11+ trigger When the active space changes. This new trigger enables more robust tracking of Desktop Space changes that are not keyboard-driven (i.e., via trackpad gestures or Mission Control interaction). Previously the The focused window changes was used; it didn't always fire when needed and fired many times when it was not needed.

  • Version 2.0 of this macro group includes many other improvements detailed below.

macro: Go to Application Desktop

  • Added a Title to the Prompt With List

macro: Go to Previous Desktop

  • Uses the new Keyboard Maestro Version 11+ trigger: When the active space changes (replacing the The focused window changes).
  • Changed ⌃⌥Return to display the Previous Desktop Space in large text (replacing a notification).
  • Added option to generate a sound when the space is changed. Note that the volume can be changed or the action can be disabled.

macro: Move Application Window to Current Desktop

  • The click-and-hold is done using absolute position because, during testing, it was found that with some applications front window top-left corner did not work as expected. Note: Thus far, this does not seem to be an issue if the application is already in the active Desktop Space, therefore the absolute position adjustment has not been made in macros: Move Window Left, Move Window Right, and Move Window to Previous Desktop.
  • For the Pause after the click-and-hold, changed from 0.2 to 0.5; for the Pause before the mouse release, changed from 1.0 to 1.5.

macro: Move Window Left a Desktop

  • For the Pause after the click-and-hold, changed from 0.2 to 0.5; for the Pause before the mouse release, changed from 1.0 to 1.5.

macro: Move Window Right a Desktop

  • For the Pause after the click-and-hold, changed from 0.2 to 0.5; for the Pause before the mouse release, changed from 1.0 to 1.5.

macro: Move Window to Previous Desktop

  • For the Pause after the click-and-hold, changed from 0.2 to 0.5; for the Pause before the mouse release, changed from 1.0 to 1.5.

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

  • Removed the Front Window Check and the corresponding variable local_Null FrontWindowName.
  • When an About menu is not found, a dialog is generated (replacing a notification). The dialog includes an Edit button which enables a quick method to open this subroutine so that the application can be added to the local_MissingAboutMenuItem variable.

macro: sub—Get DesktopNo

  • Removed the local__Notification Level subroutine parameter as it is no longer needed since macro Go to Previous Desktop was changed to use Keyboard Maestro Version 11+ trigger When the active space changes (replacing trigger The focused window changes).
  • Moved the WhichSpace AppleScript to a Until action (Timeout set to 5 seconds). The approach previously was to run the AppleScript and then Pause 0.2 seconds.
  • If WhichSpace is not running, added a check that the user has been logged in for at least 2 minutes so that the WhichSpace not running dialog does not appear during mac boot up or user login.

macro: sub—Get DesktopNo_prev

  • Removed the parameter local__DefaultDesktopNo_prev.

macro: sub—Go to Desktop N

  • When attempting to move to a Desktop Space that does not exist, alert using large text (replacing a notification).

macro: sub—Move Window to Desktop N

  • Takes no action if local__TargetDesktopNo matches the curent DesktopNo.
  • For the Pause after the click-and-hold, changed from 0.2 to 0.5; for the Pause before the mouse release changed from 1.0 to 1.5.

macro: sub—Type Keystroke for Switch to Desktop N

  • Removed the otherwise notification since a failure to move to a non-existent Desktop Space is handled in the macro sub—Go to Desktop N.

v1.1 (2022-04-06) • Requires Keyboard Maestro Version 10+

macro: Go to Previous Desktop

  • Bug fix: local_dnPreviouslySaved was improperly determined.

macro: Move Window to Previous Desktop

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

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 • Requires Keyboard Maestro Version 10+


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. For macOS Ventura+, configure System Settings > Desktop & Dock > Mission Control.

    For macOS pre-Ventura, configure System Preferences > Mission Control.

  3. For macOS Ventura+, configure System Settings > Keyboard > Keyboard Shortcuts > Mission Control.

    For macOS pre-Ventura, configure System Preferences > Keyboard > Shortcuts > Mission Control.

    Specifically, the macOS Mission Control Shortcuts should be configured as follows:

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

    Note: Ideally the macOS Mission Control Shortcuts can be configured exactly as summarized above; however, if any of these shortcuts causes a conflict with an existing Keyboard Maestro hot key, the shortcuts can be customized providing the macro sub—Type Keystroke for Switch to Desktop N is similarly changed. If this becomes necessary, refer to the Type a Keystroke actions.

  4. Optionally configure System Settings > Accessibilty > Display.

  5. Click to download the following:

    Desktop Spaces • Macros to Improve Navigation and Window Management v4.0.zip (298.2 KB)

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

8 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 having 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.