MACROS: Desktop Spaces β€’ Macros for Navigation and Window Management, v1.1 (Superseded)



The macros in this thread prior to 10-March-2021 have been superseded with significantly improved versions. Refer to this entry for more information.


On my MacBook Pro I use up to 15 Desktop Spaces. With several, I've assigned applications to specific spaces, e.g., I've assigned Messages to Desktop 3. With the relatively small footprint of a laptop, the combination of multiple Desktop Spaces and keyboard shortcuts greatly reduces the time and mousing required to manage windows.

Although space-assignment for some apps is certainly beneficial, there are times when windows from these apps need to be visible on other spaces. Movement of such windows with native macOS features is somewhat clumsy, and certainly not done quickly.

Keyboard Mastro, and this set of macros, can greatly assist with this macOS shortcoming and other common Desktop Space tasks even if only a few Desktop Spaces are being used.

Note that all macros below will install in a disabled Keyboard Maestro group named .sys Spaces. The individual macros will be enabled, but they will not be available until the group is enabled.


Macro: Move Window to a Space

The purpose of this macro is to move the active window to a Mission Control Desktop Space.

More
  Move window to Space 1           βŒƒβŒ₯1
  Move window to Space 2           βŒƒβŒ₯2
  Move window to Space 3           βŒƒβŒ₯3
  Move window to Space 4           βŒƒβŒ₯4
  Move window to Space 5           βŒƒβŒ₯5
  Move window to Space 6           βŒƒβŒ₯6
  Move window to Space 7           βŒƒβŒ₯7
  Move window to Space 8           βŒƒβŒ₯8
  Move window to Space 9           βŒƒβŒ₯9
  Move window to Space 10          βŒƒβŒ₯A
  Move window to Space 11          βŒƒβŒ₯B
  Move window to Space 12          βŒƒβŒ₯C
  Move window to Space 13          βŒƒβŒ₯D
  Move window to Space 14          βŒƒβŒ₯E
  Move window to Space 15          βŒƒβŒ₯F
  Move window left a space         βŒƒβŒ₯←
  Move window right a space        βŒƒβŒ₯β†’

The macro completes the move by invoking a click-and-hold on the window followed by typing the keystroke that moves to the target space. Then after a short pause, the click is released and the mouse is returned to its original position.

PREREQUISITE SETTINGS
This macro requires up to the 11 native macOS keyboard shortcuts below to be set (System Preferences > Keyboard > Shortcuts > Mission Control). The first two must always be set. As up to 9 Desktop Spaces are added, shortcuts ^1 through ^9 must be set. For Spaces 10 to 15, no additional shortcuts are required.

  Move left a space                βŒƒβŒ₯⇧Q
  Move right a space               βŒƒβŒ₯⇧W
  Switch to Desktop 1              βŒƒ1
  Switch to Desktop 2              βŒƒ2
  Switch to Desktop 3              βŒƒ3
  Switch to Desktop 4              βŒƒ4
  Switch to Desktop 5              βŒƒ5
  Switch to Desktop 6              βŒƒ6
  Switch to Desktop 7              βŒƒ7
  Switch to Desktop 8              βŒƒ8
  Switch to Desktop 9              βŒƒ9

ACKNOWLEDGEMENTS
Note: The logic in this macro was taken from macro: Move Frontmost Window to a Different Space

For more information, refer to @Tom's forum post: Move Frontmost Window to a Different Space - Macro Library - Keyboard Maestro Discourse

Three modifications were made for this macro:

  1. Expanded to accommodate up to 15 Desktop Spaces

  2. Changed the βŒƒβŒ₯Left Arrow and βŒ₯Right Arrow actions. For the rationale, see Peter Lewis’ comment in the forum post.

  3. Added logic to exit immediately if the shortcut is invoked in a space without windows

Due to the above changes, the name was changed from Move Frontmost Window to a Different Space to Move Window to a Space.

TESTED WITH
β€’ Big Sur, v11.2.1
β€’ Keyboard Maestro, v9.2
β€’ MacBookPro16,1

VERSION HISTORY
1.0 - Initial version
1.1 - Added back triggers: βŒƒβŒ₯← and βŒƒβŒ₯β†’. They were mistakenly removed before v1.0 was uploaded.

 

Move Window to a Space, v1.1, 2021-02-25

DOWNLOAD Macro File:
.sys Spaces Macros.kmmacros (272 KB)

Although not required for the operation of any other macros in this post, these two simple macros, in combination with the native macOS keyboard shortcuts, can be used to navigate a Desktop Space left (βŒƒβ†) and right (βŒƒβ†’), respectively.

Move Left a Space, v1.1, 2021-02-26

  Move left a space                βŒƒβŒ₯←

DOWNLOAD Macro File:
Move Left a Space.kmmacros (4.7 KB)

Move Right a Space, v1.1, 2021-02-26

  Move right a space               βŒƒβŒ₯β†’

DOWNLOAD Macro File:
Move Right a Space.kmmacros (4.7 KB)


Macro: Return to Previous Space

This macro is used to return to the previously displayed Mission Control Desktop Space. For example, if Space 5 is visible and ^2 is used to move to Space 2, ^Return will return to Space 5. Since a second ^Return will move back to Space 2, this macro makes it easy to toggle between two Desktop Spaces, even if they are nonadjacent.

More
  Return to Previous Space        βŒƒReturn

Note 1: Fundamental to the operation of this macro, the current Desktop Space number must be determined. This value is not available using native Keyboard Maestro actions because, according the this comment from @peterlewis, the space number is not available via a public API. Therefore, a third-party menubar open source tool named WhichSpace and AppleScript are required for this macro. See below for more information.

Note 2: This macro records a space change by detecting if the focused window title changes. Generally this works well because a user is typically toggling between spaces to use different applications or at least different windows within the same application. If however, a user changes to another space that has the same foreground window active, the previous space will not be updated, thus this macro may return to an unexpected space when it is invoked by ^Return.

PREREQUISITE SETTINGS
This macro requires up to the 11 native macOS keyboard shortcuts below to be set (System Preferences > Keyboard > Shortcuts > Mission Control). The first two must always be set. As up to 9 Desktop Spaces are added, shortcuts ^1 through ^9 must be set. For Spaces 10 to 15, no additional shortcuts are required.

  Move left a space                βŒƒβŒ₯⇧Q
  Move right a space               βŒƒβŒ₯⇧W
  Switch to Desktop 1              βŒƒ1
  Switch to Desktop 2              βŒƒ2
  Switch to Desktop 3              βŒƒ3
  Switch to Desktop 4              βŒƒ4
  Switch to Desktop 5              βŒƒ5
  Switch to Desktop 6              βŒƒ6
  Switch to Desktop 7              βŒƒ7
  Switch to Desktop 8              βŒƒ8
  Switch to Desktop 9              βŒƒ9

OTHER DEPENDENCY
The free open source application WhichSpace must be running. The author of WhichSpace, George Christou, kindly told me that the value displayed by WhichSpace could be returned using the AppleScript included within this Marco.

LIMITATIONS
As configured, this macro is limited to Desktop Spaces 1 to 15.

TESTED WITH
β€’ Big Sur, v11.2.1
β€’ Keyboard Maestro, v9.2
β€’ MacBookPro16,1

VERSION HISTORY
1.0 - Initial version
1.1 - Updated the macro name in this comment from Return to the Previous Space to Return to Previous Space. There is no functional change to this macro.

 

Return to Previous Space, v1.1, 2021-02-26

DOWNLOAD Macro File:
Return to Previous Space.kmmacros (41 KB)


Macro: Move Window to the Previous Space

The purpose of this macro is to move the active window to the previously displayed Mission Control Desktop Space.

More
  Move window to Previous Space    βŒƒβŒ₯Return

For example, if Space 5 is visible and ^2 is used to move to Space 2, ^βŒ₯Return will return the active window on Space 2 to Space 5. Since a second ^βŒ₯Return will move the window back to Space 2, this macro makes it easy to toggle between two Desktop Spaces, even if they are nonadjacent.

Note: Fundamental to the operation of this macro, the current Desktop Space number must be determined. This value is not available using native Keyboard Maestro actions because, according the this comment from @peterlewis, the space number is not available via a public API. Therefore, a third-party menubar open source tool named WhichSpace and AppleScript are required for this macro, See below for more information.

PREREQUISITE SETTINGS
This macro requires up to the 11 native macOS keyboard shortcuts below to be set (System Preferences > Keyboard > Shortcuts > Mission Control). The first two must always be set. As up to 9 Desktop Spaces are added, shortcuts ^1 through ^9 must be set. For Spaces 10 to 15, no additional shortcuts are required.

  Move left a space                βŒƒβŒ₯⇧Q
  Move right a space               βŒƒβŒ₯⇧W
  Switch to Desktop 1              βŒƒ1
  Switch to Desktop 2              βŒƒ2
  Switch to Desktop 3              βŒƒ3
  Switch to Desktop 4              βŒƒ4
  Switch to Desktop 5              βŒƒ5
  Switch to Desktop 6              βŒƒ6
  Switch to Desktop 7              βŒƒ7
  Switch to Desktop 8              βŒƒ8
  Switch to Desktop 9              βŒƒ9

OTHER DEPENDENCY
The free open source application WhichSpace must be running. The author of WhichSpace, George Christou, kindly told me that the value displayed by WhichSpace could be returned using the AppleScript included within this Marco.

LIMITATIONS
As configured, this macro is limited to Desktop Spaces 1 to 15.

TESTED WITH
β€’ Big Sur, v11.2.1
β€’ Keyboard Maestro, v9.2
β€’ MacBookPro16,1

VERSION HISTORY
1.0 - Initial version

 

Move Window to the Previous Space, v1.0, 2021-02-26

DOWNLOAD Macro File:
Move Window to the Previous Space.kmmacros (35 KB)


Macro: Move Window to the Current Space

The purpose of this macro is to move a window from another Mission Control Desktop Space to the Current Space.

More
  Move window to Current Space     βŒƒβŒ₯0

When the macro is invoked, a list of running applications will appear in a prompt list. When one of the applications is selected, the macro will move to the Space where the active window for the applications resides. The window will then be moved to the Current Space.

The window move is done by invoking a click-and-hold on the upper-left of the window followed by typing the keystroke that moves back to the Current Space. Then after a short pause, the click is released and the mouse is returned to its original position.

PREREQUISITE SETTINGS
This macro requires up to the 11 native macOS keyboard shortcuts below to be set (System Preferences > Keyboard > Shortcuts > Mission Control). The first two must always be set. As up to 9 Desktop Spaces are added, shortcuts ^1 through ^9 must be set. For Spaces 10 to 15, no additional shortcuts are required.

  Move left a space                βŒƒβŒ₯⇧Q
  Move right a space               βŒƒβŒ₯⇧W
  Switch to Desktop 1              βŒƒ1
  Switch to Desktop 2              βŒƒ2
  Switch to Desktop 3              βŒƒ3
  Switch to Desktop 4              βŒƒ4
  Switch to Desktop 5              βŒƒ5
  Switch to Desktop 6              βŒƒ6
  Switch to Desktop 7              βŒƒ7
  Switch to Desktop 8              βŒƒ8
  Switch to Desktop 9              βŒƒ9

OTHER DEPENDENCIES

  1. The free open source application WhichSpace must be running. The author of WhichSpace, George Christou, kindly told me that the value displayed by WhichSpace could be returned using the AppleScript included within this Marco.

  2. The Activate an Application by Name action, available by a custom Keyboard Maestro Plug-in, by @ComplexPoint, must be installed.

LIMITATIONS
As configured, this macro is limited to Desktop Spaces 1 to 15.

TESTED WITH
β€’ Big Sur, v11.2.1
β€’ Keyboard Maestro, v9.2
β€’ MacBookPro16,1

VERSION HISTORY
1.0 - Initial version

 

Move Window to the Current Space, v1.0, 2021-02-22

DOWNLOAD Macro File:
Move a Window to the Current Space.kmmacros (36 KB)


SUMMARY OF NATIVE KEYBOARD SHORTCUTS AND MACRO HOT KEYS

The macOS native keyboard shortcuts and macros detailed above will enable efficient spaces navigation and window movement. This section includes a summary.

More

Space Navigation

  Move left a space                βŒƒβ†
  Move right a space               βŒƒβ†’
  Switch to Desktop 1              βŒƒ1
  Switch to Desktop 2              βŒƒ2
  Switch to Desktop 3              βŒƒ3
  Switch to Desktop 4              βŒƒ4
  Switch to Desktop 5              βŒƒ5
  Switch to Desktop 6              βŒƒ6
  Switch to Desktop 7              βŒƒ7
  Switch to Desktop 8              βŒƒ8
  Switch to Desktop 9              βŒƒ9
  Return to Previous Space         βŒƒReturn

Navigation beyond Space 9 can be done two ways:

  1. βŒƒ9, followed by the required number of βŒƒβ†’

  2. By opening or activating an application that has been assigned to the space. (This method can be used for any of the 15 spaces.)

Window Movement

  Move window left a space         βŒƒβŒ₯←
  Move window right a space        βŒƒβŒ₯β†’
  Move window to Current Space     βŒƒβŒ₯0
  Move window to Space 1           βŒƒβŒ₯1
  Move window to Space 2           βŒƒβŒ₯2
  Move window to Space 3           βŒƒβŒ₯3
  Move window to Space 4           βŒƒβŒ₯4
  Move window to Space 5           βŒƒβŒ₯5
  Move window to Space 6           βŒƒβŒ₯6
  Move window to Space 7           βŒƒβŒ₯7
  Move window to Space 8           βŒƒβŒ₯8
  Move window to Space 9           βŒƒβŒ₯9
  Move window to Space 10          βŒƒβŒ₯A
  Move window to Space 11          βŒƒβŒ₯B
  Move window to Space 12          βŒƒβŒ₯C
  Move window to Space 13          βŒƒβŒ₯D
  Move window to Space 14          βŒƒβŒ₯E
  Move window to Space 15          βŒƒβŒ₯F
  Move window to Previous Space    βŒƒβŒ₯Return

MANAGING SPACES WITH A PALETTE

If the macros in this section are downloaded and installed (along with the macros from above), the functions described above can be optionally controlled using one hot key (default: ^S) which opens a Keyboard Maestro Conflict Palette.

More

Because it is a little faster, I normally navigate spaces and complete related window moves using the macOS native Mission Control Keyboard Shortcuts and the hot keys defined within the four macros above; but the palette can be helpful for a new user.

TESTED WITH
β€’ Big Sur, v11.2.1
β€’ Keyboard Maestro, v9.2
β€’ MacBookPro16,1

VERSION HISTORY
1.0 - Initial version
2.0 - a) Added a palette macro to launch the new macro above named Move Window to the Previous Space. b) Added palette spacers/labels to annotate the palette entries.

 

Managing Spaces with a Palette, v2.0, 2021-02-26

The following download will replace all of the palette macros from Version 1.0; thus before downloading and installing, move to the Keyboard Maestro group named .sys Spaces and delete all of the macros that are triggered by ^S.

DOWNLOAD Macro Files (36):
sys Spaces Macros.kmmacros (151.6 KB)

11 Likes

Update: Move Window to a Space

Just updated OP with this fix.

1 Like

Updates: Move Left a Space and Move Right a Space

VERSION HISTORY
1.0 - Initial version
1.1 - Changed the Pause to 0.8 Seconds. With a shorter pause, the spaces would sometimes skip two positions when the arrow key was momentarily pressed.

Just updated OP with this fix to both macros.

1 Like

Update: Return to Previous Space

VERSION HISTORY
1.0 - Initial version
1.1 - Updated the macro name in this comment from Return to the Previous Space to Return to Previous Space. There is no functional change to this macro.

New: Move Window to the Previous Space

VERSION HISTORY
1.0 - Initial version

Update: Managing Spaces with a Palette

VERSION HISTORY
1.0 - Initial version
2.0 - a) Added a palette macro to launch the new macro above named Move Window to the Previous Space. b) Added palette spacers/labels to annotate the palette entries.

Just updated OP with these changes and additions.

Version History




All information below refers to the macros that were originally shared in this thread.


v1.1 (2021-10-22) The nine primary macros have been updated as follows:

Modified WhichSpace AppleScript to handle cases where the window title is returned as a triplet, that is: n, missing value, missing value
I've never observed a triplet, but @seishonagon did and suggested this change which accommodates those that do and has no effect on those that don't.

Macro Move Application Window to Current Desktop has one additional modification:

Removed use of custom Keyboard Maestro Plug-in action (Activate an Application by Name) and replaced with Execute Shell Script command (open -a "$KMVAR_local__AppSelected”) followed by AppleScript command (activate).

This modification was suggested by @ccstone via a PM. He also suggested the zip file organization used below.

The optional forty-two Conflict Palette macros have not been modified.

v1.0 (2021-03-10) Initial version


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.
  • 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.


Quick Start

If you previously installed the Desktop Spaces β€’ Macros for Navigation and Window Management macros, 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 - v1.0 superseded.

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

To install Desktop Spaces β€’ Macros for Navigation and Window Management v1.1, 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. Double-click to download the following:
    Desktop Spaces - Macros for Navigation and Window Management.zip (108.0 KB)

  5. Unzip the download; open the included folder. Three subfolders will be enclosed:

    a. [ Required Step ] Open subfolder 1 - Required. Double-click 9 Primary Macros.kmmacros to install the primary macros. Note that the macros will individually ENABLED; but the macro group .sys Desktops will be DISABLED. When ready to use the macros, enable this group.

    b. [ Optional Step ] Open subfolder 2 - Optional but highly recommended. Double-click WhichSpace – Download Latest Release.webloc to install this free, open-source application that displays the Mission Control Desktop Space number in the mac menubar. Set WhichSpace to open (start) during each login.

    c. [ Optional Step ] Open subfolder 3 - Optional but helpful for beginners or occassional users. Double-click 42 Palette Macros.kmmacros to install the optional palette macros. Note that theses macros will also be installed in the .sys Desktops macro group and be ENABLED.


Macro Improvements and Limitations

This set of macros includes several improvements:

  • Support for all 16 Mission Control Desktop Spaces
  • Macro names include the word Desktop rather than Space since this is a more mainstream term for virtual desktops
  • All macros are installed in a group named: .sys Desktops (versus .sys Spaces). Like before the macro group is initially disabled when the macros are downloaded.
  • More reliable tracking of the Current Desktop Number since a macro Go to Desktop N (Desktops 1 to 16: βŒƒ1 .. βŒƒ9, βŒƒβ‡§0 .. βŒƒβ‡§6) has been added for basic navigation
  • Moving desktop right (βŒƒRight Arrow) and desktop left (βŒƒLeft Arrow) have been improved:
    • animation gives better feedback than the macOS native Mission Control Keyboard Shortcuts Move left a space and Move right a space
    • thanks to the macro MACRO: HotKey Multi-Press Template, by @DanThomas, multiple taps on the arrow keys are recognized
    • holding arrow keys repeats the movement left or right until the keys are released
    • the previous Desktop is retained to the desktop that was active when the left or right movement began. For example, if one started on Desktop 1, and triple-tapped βŒƒRight Arrow to move to Desktop 4 (Desktop 1 -> Desktop 2 -> Desktop 3 -> Desktop 4), subsequent use of βŒƒReturn would return to Desktop 1 (not Desktop 3).
  • For the macro Move Application Window to Current Desktop (βŒƒβŒ˜0), Applications to Exclude can be configured (by default LaunchBar and Amphetamine are excluded).
  • The optional Conflict Palette (βŒƒS) displays all of the hot keys for the nine underlying macros. The forty-two Conflict Palette macros have been simplified to use the Trigger Macros by Hot Key action
  • Most of the functionality of these macros will now work independent of WhichSpace. However, without it, the following features will be absent:
    • The visual indication of the Desktop Number rendered by WhichSpace on the macOS menubar

    • The tracking of the current and previous Desktop will be temporarily inaccurate if a Desktop change occurs outside the control of the macros. For example,

      • three-finger swipe left or right on a trackpad
      • Mission Control is displayed and a Desktop is selected
      • a macOS application is opened, or activated, and an automatic Desktop change occurs (see Note 1)

      So what’s the impact? Go to Previous Desktop (βŒƒReturn) will return to the last Desktop that was displayed using a macro hot key. For example:

      1. Moved to Desktop 4 using βŒƒ4
      2. Moved to Desktop 3 using a three-finger swipe on the trackpad
      3. Moved to Desktop 1 using βŒƒ1
      4. Go to Previous Desktop (βŒƒReturn) would move to Desktop 4 (not Desktop 3)

Note 1: This can occur when the following is selected (highly recommended): System Preferences > Mission Control > When switching to an application, switch to a Space with open windows for the application


Nine Primary Macros

This section includes more information and images for each of the nine primary macros. The Go to Previous Desktop section provides a comprehensive explanation for those interested in the inner workings. This information is also available in the comments of each macro.

All macros can be downloaded as a group using the link in the Quick Start section.


Macro: Go to Desktop N

This macro is used to directly move to one of the sixteen Desktops.

Go to Desktop 1 .. 9                             βŒƒ1 .. βŒƒ9
Go to Desktop 10 .. 16                           βŒƒβ‡§0 .. βŒƒβ‡§6
More

PREREQUISITE SETTINGS
This macro requires up to the 16 native macOS keyboard shortcuts below to be set (System Preferences > Keyboard > Shortcuts > Mission Control). These must be set as Desktop Spaces are added.

Switch to Desktop 1 .. 9                         βŒƒβŒ₯⌘1 .. βŒƒβŒ₯⌘9
Switch to Desktop 10 .. 16                       βŒƒβŒ₯β‡§βŒ˜0 .. βŒƒβŒ₯β‡§βŒ˜6

TESTED WITH
β€’ Big Sur, v11.6
β€’ Keyboard Maestro, v9.2
β€’ MacBookPro16,1

VERSION HISTORY
1.0 - Initial version
1.1 - Modified WhichSpace AppleScript to handle cases where the window title is returned as a triplet, that is: n, missing value, missing value


Macro: Go Left a Desktop

This macro is used to move left one or more Desktops.

Go Left a Desktop				                 βŒƒLeft Arrow

In addition to a single press of βŒƒLeft Arrow, multiple taps of the Left Arrow (while βŒƒ is down) will be recognized. Also, if βŒƒLeft Arrow is held down, the movement left is repeated until released.

More

PREREQUISITE SETTINGS
This macro requires up to the 16 native macOS keyboard shortcuts below to be set (System Preferences > Keyboard > Shortcuts > Mission Control). These must be set as Desktop Spaces are added.

Switch to Desktop 1 .. 9                         βŒƒβŒ₯⌘1 .. βŒƒβŒ₯⌘9
Switch to Desktop 10 .. 16                       βŒƒβŒ₯β‡§βŒ˜0 .. βŒƒβŒ₯β‡§βŒ˜6

ACKNOWLEDGEMENT
The multiple-tap capability of this macro is provided by MACRO: HotKey Multi-Press Template, by @DanThomas.

TESTED WITH
β€’ Big Sur, v11.6
β€’ Keyboard Maestro, v9.2
β€’ MacBookPro16,1

VERSION HISTORY
1.0 - Initial version
1.1 - Modified WhichSpace AppleScript to handle cases where the window title is returned as a triplet, that is: n, missing value, missing value


Macro: Go Right a Desktop

This macro is used to move right one or more Desktops.

Go Right a Desktop			                     βŒƒRight Arrow

In addition to a single press of βŒƒRight Arrow, multiple taps of the Right Arrow (while βŒƒ is down) will be recognized. Also, if βŒƒRight Arrow is held down, the movement right is repeated until released.

More

PREREQUISITE SETTINGS
This macro requires up to the 16 native macOS keyboard shortcuts below to be set (System Preferences > Keyboard > Shortcuts > Mission Control). These must be set as Desktop Spaces are added.

Switch to Desktop 1 .. 9                         βŒƒβŒ₯⌘1 .. βŒƒβŒ₯⌘9
Switch to Desktop 10 .. 16                       βŒƒβŒ₯β‡§βŒ˜0 .. βŒƒβŒ₯β‡§βŒ˜6

ACKNOWLEDGEMENT
The multiple-tap capability of this macro is provided by MACRO: HotKey Multi-Press Template, by @Dan Thomas.

TESTED WITH
β€’ Big Sur, v11.6
β€’ Keyboard Maestro, v9.2
β€’ MacBookPro16,1

VERSION HISTORY
1.0 - Initial version
1.1 - Modified WhichSpace AppleScript to handle cases where the window title is returned as a triplet, that is: n, missing value, missing value


Macro: Go to Previous Desktop

This macro is used to return to the previously displayed Desktop.

Go to Previous Desktop                           βŒƒReturn

For example, if Desktop 5 is visible and βŒƒ2 is used to move to Desktop 2, βŒƒReturn will return to Desktop 5. Since a second βŒƒReturn will move back to Desktop 2, this macro makes it easy to toggle between two Desktops, even if they are nonadjacent.

Note: If the macro is triggered with the βŒ₯ (Option key) down, the desktop change will not occur; instead a brief message will appear indicating the previously displayed Desktop.

More

PREREQUISITE SETTINGS
This macro requires up to the 16 native macOS keyboard shortcuts below to be set (System Preferences > Keyboard > Shortcuts > Mission Control). These must be set as Desktop Spaces are added.

Switch to Desktop 1 .. 9                         βŒƒβŒ₯⌘1 .. βŒƒβŒ₯⌘9
Switch to Desktop 10 .. 16                       βŒƒβŒ₯β‡§βŒ˜0 .. βŒƒβŒ₯β‡§βŒ˜6

:warning:OPERATIONAL DETAILS
According to this comment from @peterlewis, the Desktop number is not available via macOS public API, thus there is no native Keyboard Maestro action for this purpose. However, when Desktop navigation and window movement are done exclusively with this macro and the others in the macro group (see Note 1) the current and previous Desktop numbers are accurately tracked and saved using Keyboard Maestro global variables. This is useful and often sufficient but there will be times when a Desktop change occurs outside the domain of the macros in this group. For example:

a. three-finger swipe left or right on a trackpad
b. Mission Control is displayed and a Desktop is selected
c. a macOS application is opened, or activated, and an automatic Desktop change occurs (see Note 2)

In these cases, and possibly others, the tracking of the current and previous Desktop will be temporarily inaccurate (see Note 3) unless a helper application named WhichSpace is installed. (This installation is highly recommended, but not required.)

WhichSpace is a free, open source application by George Christou that displays the Desktop number in the macOS menubar. This indication is of great value when using multiple Desktops. As a bonus and employed within this macro, the Desktop number can be determined with a simple AppleScript (see Note 4).

Note 1: Other macros in this macro group are:
Go Left a Desktop, Go Right a Desktop, Go to Desktop N, Move Application Window to Current Desktop, Move Window Left a Desktop, Move Window Right a Desktop, Move Window to Desktop N, Move Window to Previous Desktop)

Note 2: Automatic Desktop changes can occur when the following is selected: System Preferences > Mission Control > When switching to an application, switch to a Space with open windows for the application

Note 3: If WhichSpace is not installed, current and previous Desktops can be temporarily inaccurate. For example:

a. Move to Desktop 4 using macro Go to Desktop N (βŒƒ4).
b. Move to Desktop 3 using trackpad
c. Move to Desktop 1 using macro Go to Desktop N (βŒƒ1).
d. Running this macro, Go to Previous Desktop (βŒƒReturn) would move to Desktop 4, not Desktop 3.

Note 4: When WhichSpace is installed, this macro records a desktop change by detecting if the focused window title changes. Generally this works well because when a desktop change occurs (even outside the control of the macros in this group) different applications or at least different windows within the same application are made automatically active or selected by a user. However, there may be rare situations when a desktop change is made, outside the domain of these macros, and the window title does not change, thus the previous and current Desktop tracking will be temporarily inaccurate.

TESTED WITH
β€’ Big Sur, v11.6
β€’ Keyboard Maestro, v9.2
β€’ MacBookPro16,1

VERSION HISTORY
1.0 - Initial version
1.1 - Modified WhichSpace AppleScript to handle cases where the window title is returned as a triplet, that is: n, missing value, missing value


Macro: Move Window to Desktop N

This macro is used to move the active window to one of the sixteen Desktops.

Move Window to Desktop 1 .. 9                    βŒƒβŒ˜1 .. βŒƒβŒ˜9
Move Window to Desktop 10 .. 16                  βŒƒβ‡§βŒ˜0 .. βŒƒβ‡§βŒ˜6

The macro completes the move by invoking a click-and-hold on the upper-left of the window followed by typing the keystroke that moves to the target space. Then after a short pause, the click is released and the mouse is returned to its original position.

More

PREREQUISITE SETTINGS
This macro requires up to the 16 native macOS keyboard shortcuts below to be set (System Preferences > Keyboard > Shortcuts > Mission Control). These must be set as Desktop Spaces are added.

Switch to Desktop 1 .. 9                         βŒƒβŒ₯⌘1 .. βŒƒβŒ₯⌘9
Switch to Desktop 10 .. 16                       βŒƒβŒ₯β‡§βŒ˜0 .. βŒƒβŒ₯β‡§βŒ˜6

ACKNOWLEDGEMENT
The logic in this macro was taken from Move Frontmost Window to a Different Space, by @Tom.

Two modifications were made for this macro:

  1. Expanded to accommodate up to 16 Desktop Spaces
  2. Added logic to exit immediately if the shortcut is invoked in a space without windows

Due to the above changes, the name was changed from Move Frontmost Window to a Different Space to Move Window to Desktop N.

TESTED WITH
β€’ Big Sur, v11.6
β€’ Keyboard Maestro, v9.2
β€’ MacBookPro16,1

VERSION HISTORY
1.0 - Initial version
1.1 - Modified WhichSpace AppleScript to handle cases where the window title is returned as a triplet, that is: n, missing value, missing value


Macro: Move Window Left a Desktop

This macro is used to move the active window left one or more Desktops.

Move Window Left a Desktop                       βŒƒβŒ˜Left Arrow

In addition to a single press of βŒƒβŒ˜Left Arrow, multiple taps of the Left Arrow (while βŒƒβŒ˜ are down) will be recognized. Also, if βŒƒβŒ˜Left Arrow is held down, the movement left is repeated until released.

More

PREREQUISITE SETTINGS
This macro requires up to the 16 native macOS keyboard shortcuts below to be set (System Preferences > Keyboard > Shortcuts > Mission Control). These must be set as Desktop Spaces are added.

Switch to Desktop 1 .. 9                         βŒƒβŒ₯⌘1 .. βŒƒβŒ₯⌘9
Switch to Desktop 10 .. 16                       βŒƒβŒ₯β‡§βŒ˜0 .. βŒƒβŒ₯β‡§βŒ˜6

ACKNOWLEDGEMENT
The multiple-tap capability of this macro is provided by MACRO: HotKey Multi-Press Template, by @DanThomas.

TESTED WITH
β€’ Big Sur, v11.6
β€’ Keyboard Maestro, v9.2
β€’ MacBookPro16,1

VERSION HISTORY
1.0 - Initial version
1.1 - Modified WhichSpace AppleScript to handle cases where the window title is returned as a triplet, that is: n, missing value, missing value


Macro: Move Window Right a Desktop

This macro is used to move the active window right one or more Desktops.

Move Window Right a Desktop                      βŒƒβŒ˜Right Arrow

In addition to a single press of βŒƒβŒ˜Right Arrow, multiple taps of the Right Arrow (while βŒƒβŒ˜ are down) will be recognized. Also, if βŒƒβŒ˜Right Arrow is held down, the movement left is repeated until released.

More

PREREQUISITE SETTINGS
This macro requires up to the 16 native macOS keyboard shortcuts below to be set (System Preferences > Keyboard > Shortcuts > Mission Control). These must be set as Desktop Spaces are added.

Switch to Desktop 1 .. 9                         βŒƒβŒ₯⌘1 .. βŒƒβŒ₯⌘9
Switch to Desktop 10 .. 16                       βŒƒβŒ₯β‡§βŒ˜0 .. βŒƒβŒ₯β‡§βŒ˜6

ACKNOWLEDGEMENT
The multiple-tap capability of this macro is provided by MACRO: HotKey Multi-Press Template, by @DanThomas.

TESTED WITH
β€’ Big Sur, v11.6
β€’ Keyboard Maestro, v9.2
β€’ MacBookPro16,1

VERSION HISTORY
1.0 - Initial version
1.1 - Modified WhichSpace AppleScript to handle cases where the window title is returned as a triplet, that is: n, missing value, missing value


Macro: Move Window to Previous Desktop

This macro is used to move the active window to the previously displayed Desktop.

Move Window to Previous Desktop                  βŒƒβŒ˜Return

For example, if Space 5 is visible and βŒƒ2 is used to move to Space 2, βŒƒβŒ˜Return will move the active window on Space 2 to Space 5. Since a second βŒƒβŒ˜Return will move the window back to Space 2, this macro makes it easy to toggle between two Desktop Spaces, even if they are nonadjacent.

Note: If the macro is triggered with the βŒ₯ (Option key) down, the window move will not occur; instead a brief message will appear indicating the previously displayed Desktop.

More

PREREQUISITE SETTINGS
This macro requires up to the 16 native macOS keyboard shortcuts below to be set (System Preferences > Keyboard > Shortcuts > Mission Control). These must be set as Desktop Spaces are added.

Switch to Desktop 1 .. 9                         βŒƒβŒ₯⌘1 .. βŒƒβŒ₯⌘9
Switch to Desktop 10 .. 16                       βŒƒβŒ₯β‡§βŒ˜0 .. βŒƒβŒ₯β‡§βŒ˜6

TESTED WITH
β€’ Big Sur, v11.6
β€’ Keyboard Maestro, v9.2
β€’ MacBookPro16,1

VERSION HISTORY
1.0 - Initial version
1.1 - Modified WhichSpace AppleScript to handle cases where the window title is returned as a triplet, that is: n, missing value, missing value


Macro: Move Application Window to Current Desktop

This macro is used to move a window from another Desktop to the current Desktop.

Move Application Window to Current Desktop       βŒƒβŒ˜0

When the macro is invoked, a list of running applications will appear in a prompt list. When one of the applications is selected, the macro will move to the Desktop where the active window for the applications appears. The window will then be moved to the current Desktop.

The window move is done by invoking a click-and-hold on the upper-left of the window followed by typing the key stroke that moves back to the current Desktop. Then after a momentary pause, the click is released and the mouse is returned to its original position.

More

PREREQUISITE SETTINGS
This macro requires up to the 16 native macOS keyboard shortcuts below to be set (System Preferences > Keyboard > Shortcuts > Mission Control). These must be set as Desktop Spaces are added.

Switch to Desktop 1 .. 9                         βŒƒβŒ₯⌘1 .. βŒƒβŒ₯⌘9
Switch to Desktop 10 .. 16                       βŒƒβŒ₯β‡§βŒ˜0 .. βŒƒβŒ₯β‡§βŒ˜6

:warning:OTHER DEPENDENCIES
a. The following must be set: System Preferences > Mission Control > When switching to an application, switch to a Space with open windows for the application.
b. The Activate an Application by Name action, available by a custom Keyboard Maestro Plug-in, by @ComplexPoint, must be installed.

OPERATIONAL DETAILS
When the window switching occurs during the execution of this macro, the previous Desktop will be updated to the Desktop that includes the target application window (see Note 1). This default behavior is normally desirable as it makes it easy to return the moved window to its original Desktop using the macro Move Window to Previous Space (βŒƒβŒ˜Return).

In rare situations it may be desirable to preserve the pre-window move previous Desktop. To do this, include the βŒ₯ (Option) key when launching this shortcut.

Note 1: If WhichSpace is not running, this update of the previous Desktop will not occur. For more WhichSpace information, refer to the the macro Go to Previous Desktop.

APPLICATIONS TO EXCLUDE
By default, this macro will ignore two applications: LaunchBar and Amphetamine. To add others, modify the list in the specified action below.

TESTED WITH
β€’ Big Sur, v11.6
β€’ Keyboard Maestro, v9.2
β€’ MacBookPro16,1

VERSION HISTORY
1.0 - Initial version
1.1 - Modified WhichSpace AppleScript to handle cases where the window title is returned as a triplet, that is: n, missing value, missing value

7 Likes

Hi,
I'm trying to use your - brilliant - macro system but I'm running into an issue:

It seems that the Variable jsMCDS_DesktopNo is set to 1, missing variable and thus all the "go right", "go left", "go to previous" macros fail.


The Variable jsMCDS_DesktopNo_Prev doesn't even seem to be set anywhere.


I've tried to install it from scratch twice and the same thing happens each time.

Not sure how to debug. Can you help?

Thanks!

@seishonagon, let's try a few basics first. If that doesn't work, then I'll need a more detailed description of the steps you are trying to execute.

  • WhichSpace must be running.
  • The macOS Keyboard Shortcuts (e.g., βŒƒβŒ₯⌘1...) must be defined.
  • jsMCDS__DesktopNo_prev (note the two underscores after jsMCDS) will not be defined immediately after installing the macros and related components. Try this: βŒƒβŒ₯⌘1 to move to Desktop 1; βŒƒβŒ₯⌘2 to move to Desktop 2. Then jsMCDS__DesktopNo_prev should be defined and βŒƒReturn should function as expected.

Hey, thanks for your reply.

  • WhichSpace is running
  • The keyboard shortcuts are defined as needed up to number 16
  • moving to spaces by their number (e.g. βŒƒβŒ₯⌘1, βŒƒβŒ₯⌘2 ...) works as expected.

But if I type βŒƒβŒ₯Return, I get the following dialog:

and then an error message:

Sorry, in my previous reply I should have said:

Try this: βŒƒ1 to move to Desktop 1 ; βŒƒ2 to move to Desktop 2 . Then jsMCDS__DesktopNo_prev should be defined and βŒƒReturn should function as expected.

Note: βŒƒn ( n = 1 to 16 ) will save the previous desktop to the jsMCDS__DesktopNo_prev variable (even if WhichSpace is not running).

Nope ... but I think I got it:
In script debugger, your line:

tell application "System Events" to tell process "WhichSpace" to title of menu bar items of menu bar 1

returns a triplet, e.g: 2, missing value, missing value
which is then saved to a KM variable which SHOULD be an integer but is a string. And so subsequent calculations don't work.
Replacing the script by

tell application "System Events" Β¬
to tell process "WhichSpace" Β¬
to set temp to (title of menu bar items of menu bar 1)
return item 1 of temp

works like a charm!

I don't know if the AS dictionnary of WhichSpace changed, ( I installed it via homebrew?) but there you have it !

Thanks for the set of macros, very useful.

Ha, I was about to send you a macro that isolated the AppleScript for WhichSpace. But you were one step ahead of me! I love it! As a reward, you get to run these macros at half price! :wink:

(I was thinking that the AppleScript was failing, but not as you discovered. As written, the macros will run when WhichSpace is not running, but not when WhichSpace returns the unexpected results.)

Interesting, not on my systems. I'm running WhichSpace 0.3.2 and Keyboard Maestro 9.2 on two systems:

  • 16" MBPro, macOS 11.2.3
  • Mac mini (Late 2012), macOS 10.14.6

But regardless, as I'm sure you know, the modified AppleScript will run just fine on systems that only return one value.

One word of caution: You will find this AppleScript sprinkled throughout the macros so your fix will need to inserted several places. As you go through the macros, be sure to expand each section.

Several weeks ago, @ccstone gave me some constructive feedback and suggested a simplified install process. Now I have another reason to get that done. All I need is a little free time. :grinning:

You're welcome, @seishonagon. Thanks for the messages and troubleshooting.

Hi jims, thanks for the great work!
The conflict palette is especially useful.

To display it in two column, need to set the Conflict Palette Style in the Preferences of Keyboard Maestro.

@xsmk, glad you like it. Thanks for the comment.

Thanks for pointing that out. When I repost this with a few refinements, I'll add that comment.

For the sake of argument, let's say that the shortcuts that you have me set my mission control shortcuts to are taken (these guys):

Switch to Desktop 1 .. 9 βŒƒβŒ₯⌘1 .. βŒƒβŒ₯⌘9
Switch to Desktop 10 .. 16 βŒƒβŒ₯β‡§βŒ˜0 .. βŒƒβŒ₯β‡§βŒ˜6

Digging around the various macros in the bundle of 9.... I found *Switch of Text β€œ%Variable%local__dnTarget%” sections to be the only thing referencing those short cuts. Assuming I go update that section, in each of the 9 macros, are there any other places that I need to go update to make match the shortcuts that I did set my "Switch Desktop" commands to?

i.e.

If it is β€œ1”, Execute the Following Actions:

  • [image]Type the βŒƒβŒ₯⌘1 Keystroke[image]

If it is β€œ2”, Execute the Following Actions:

  • [image]Type the βŒƒβŒ₯⌘2 Keystroke[image]

If it is β€œ3”, Execute the Following Actions:

  • [image]Type the βŒƒβŒ₯⌘3 Keystroke[image]

If it is β€œ4”, Execute the Following Actions:

  • [image]Type the βŒƒβŒ₯⌘4 Keystroke[image]

If it is β€œ5”, Execute the Following Actions:

  • [image]Type the βŒƒβŒ₯⌘5 Keystroke[image]

If it is β€œ6”, Execute the Following Actions:

  • [image]Type the βŒƒβŒ₯⌘6 Keystroke[image]

If it is β€œ7”, Execute the Following Actions:

  • [image]Type the βŒƒβŒ₯⌘7 Keystroke[image]

If it is β€œ8”, Execute the Following Actions:

  • [image]Type the βŒƒβŒ₯⌘8 Keystroke[image]

If it is β€œ9”, Execute the Following Actions:

  • [image]Type the βŒƒβŒ₯⌘9 Keystroke[image]

If it is β€œ10”, Execute the Following Actions:

  • [image]Type the βŒƒβŒ₯β‡§βŒ˜0 Keystroke[image]

If it is β€œ11”, Execute the Following Actions:

  • [image]Type the βŒƒβŒ₯β‡§βŒ˜1 Keystroke[image]

If it is β€œ12”, Execute the Following Actions:

  • [image]Type the βŒƒβŒ₯β‡§βŒ˜2 Keystroke[image]

If it is β€œ13”, Execute the Following Actions:

  • [image]Type the βŒƒβŒ₯β‡§βŒ˜3 Keystroke[image]

If it is β€œ14”, Execute the Following Actions:

  • [image]Type the βŒƒβŒ₯β‡§βŒ˜4 Keystroke[image]

If it is β€œ15”, Execute the Following Actions:

  • [image]Type the βŒƒβŒ₯β‡§βŒ˜5 Keystroke[image]

If it is β€œ16”, Execute the Following Actions:

  • [image]Type the βŒƒβŒ₯β‡§βŒ˜6 Keystroke[image]

Thanks!

Geoff

@Geoffrey_Blotter, no guarantees, but I think you have identified the only change required. I'm sure you noticed that this Switch/Case macro action is distringuished by the purple color. So if you modify the keystrokes within one macro, you can copy that modified Switch/Case action and paste it in the other eight macros (of course also discarding the original Switch/Case actions in each of the eight target macros). The copy-and-paste technique will greatly minimize the work and reduce the probability of an improper modification.

You're a gem for pointing that out, thanks!

I'll make that change and try and report back if that was the only place or not, for documentation purposes. Thanks @_jims!

Out of curiosity, and a fear of making unintended downstream impacts... Can I ask why the first step is to go remap default keyboard shortcuts for switching to different Desktops? i.e. why not use the default values?

Thanks again for making this killer set of macros :blush:

Yes, this seems like a bit of a pain, but there is a good reason.

These macros track the current and previous desktop space number (1 to 16). This, however, can only be done reliably if desktop navigation is done exclusively using one of the nine macros. That is, if one were to change spaces using one of the macOS native Switch to Desktop N (1 to 16) shortcuts (or a trackpad gesture), the current and previous values would be temporarily incorrect. That's why users should use the Go to Desktop N macro rather than the native macOS Switch to Desktop N shortcuts to change desktop spaces. The key remapping suggested in the Quick Start (more complex key combinations), and the macro hot keys used in the nine macros (more intuitive key combinations) encourage this behavior.

Here's some related information...

Within Keyboard Maestro, macro-initiated desktop space changes can only be accomplished with the Type a Keystroke action. That is, Keyboard Maestro does not provide change desktop actions. This is for good reason: Apple does not provide a corresponding public API. Due to the missing API, desktop space changes also cannot be done with an AppleScript or a shell script.

Finally, Keyboard Maestro does not provide a token or action to determine the current desktop space. Same reason: no API. Thus, a third-party tool named WhichSpace is used in the macros (via AppleScript) to determine the current desktop space.

@seishonagon, I was never able to reproduce the issue you identified; nevertheless, I implemented your change into v1.1 of the macros.

Thanks again!

1 Like

@xsmk, I added this comment into a Read Me First file that is downloaded with v1.1 of the macros.

Thanks again for your feedback!