Move Frontmost Window to a Different Space

If this topic title sounds familiar to anyone: yes there has been a similar topic about two years ago, and I’ve posted the ancestor of this macro there.

I’m still using the macro daily, and with the time it has improved and gained functionality.

So here the current version.

Purpose

The macro allows to move the frontmost window of (almost) any app…

  • one or more Spaces right or left
  • to a specific Space

…by using only the keyboard.

Usage

Default configuration of the macro:

Shortcut Result
1 Move window to Space 1
2 Move window to Space 2
3 Move window to Space 3
4 Move window to Space 4
Move window right (one or more Spaces)
Move window left (one or more Spaces)

  • If you press one of the Arrow shortcuts, then the window will move one Space each time.
  • If you hold down one of the Arrow shortcuts, then the window will quickly move through the Spaces until it reaches the last one.

How it works

The macro moves the mouse to the toolbar of the window, performs a click-and-hold, and activates one of Mission Control’s Space movement shortcuts (see below). After that, it moves the mouse back to the previous position.

Requirements

  • The macro relies on Mission Control’s standard shortcuts for moving between Spaces (at least I think these are the standard settings):

    05-pty-fs8

    • If you have modified these shortcuts then you have to adapt the macro.
  • Depending on your computer it might be necessary to adjust the length of the pause (third to last action).

Alternatives

  • BetterTouchTool offers similar functionality.
  • SizeUp (untested)
  • macOS: Open Mission Control and drag a window to one of the Spaces/Desktops at the top.
  • macOS: Click and hold the toolbar of a window and at the same time press one of Mission Control’s Space shortcuts. (That’s what the macro is doing for you.)

Move Frontmost Window to a Different Space.kmmacros (10.6 KB)

6 Likes

Again a great macro :wink:
Thanks for creating and sharing :+1:

Indeed, as mentioned in another thread, currently the Arrow Key part odf the macro is not working. Probably since Mojave (though I thought it was still working shortly after installing Mojave, but my memory may fool me :thinking: ).

Anyway, something with the handling of the Arrow keys seems to have changed. I’m trying to find a solution. If anybody (maybe @peternlewis?) has an idea, please don’t hesitate to post.

1 Like

Hallo @Tom, thank you so much in advance. Since I always use your macro in connection with the numbers (⌥ ⌃+1 etc.), I had not noticed that yet.
Unfortunately, Mojave has its quirks, as the unreliable functioning of the F-keys shows.

Honestly, I don’t know what’s going on.

I removed the Arrows and any modifiers from the directional trigger keys (e.g. Move Window to one Space Left: F3), and replaced the directional Mission Control movement shortcuts in System Preferences by something without Arrows (e.g. Move left a space ⌃⌥⌘F5), and it still doesn’t work,

When I do it manually, that is, mouse-click-holding a window, then typing the MC movement key as in System Preferences, then it works.

So, ATM, to me it seems that the Hold Mouse Button action is somehow incompatible with that Mission Control movement (or gets cancelled by it), no matter which trigger keys or which Mission Control shortcuts are used.

Somehow astonishing that the non-directional commands (⌃⌥1 etc.) still work.

1 Like

Then I propose, for the time being, that you remove the unneeded (and dysfunctional) triggers from the macro, and ideally also the dysfunctional actions.

Unfortunatly I mostly used the ⌃⌥⇢ and ⌃⌥⇠ functions :frowning:
(It was sooo convenient to move a window one Space right with ⌃⌥⇢, then just releasing the ⌥ key, while keeping the other finger on ⌃, and tapping ⇠ (to go back to the previous Space), select the next window, rinse and repeat…)

Thank you for the feedback @Tom :+1:
I'll leave it as it is, because I have not given up hope that Apple will improve/correct this in one of the upcoming updates here.

In Mojave, if you have (in the system or another application) a hot key that is not a modifier-letter combination (specifically that is modifier-arrow, or modifier-function key), and if you use the Type a Keystroke action to simulate that key, the key will not trigger the system/application function.

So since you changed from an arrow to F3, it still does not work.

Change the system hot key to ⌘⌥⇧⌃Q & W (or whatever letters you want).
Make a Keyboard Maestro macro for hot key ⌃⌥⇢ to type a keystroke ⌘⌥⇧⌃W, and use that same type a keystroke anywhere else if you want to change spaces from Keyboard Maestro.

1 Like

Then just disable the dysfunctional actions, but remove the unneeded triggers. You never know how a macro will interfere with other things when the environment changes, and in that case the less triggers (and actions) the macro has, the better.

And “environment changes” also includes future macros (or Automator workflows, AppleScripts) you will write, or add.

1 Like

I've already blocked it.Thank you very much for the explanation.
Thank you again for the great macro. Use it every day and I am grateful that you have shared it.

Hold on, @peternlewis has just given us a potentially great hint… (See his post above).

I just read it. I will implement it.
Thanks @peternlewis for the tip :clap:

OK, thanks, @peternlewis ; this works, with some additional complications. But it works :slight_smile:

Will post the the new macro in a couple of minutes.

OK, here is the new macro:

In the screenshot the new stuff is green, the changed stuff is yellow:

Notes:

The shortcuts in the yellow marked actions must correspond to the shortcuts in your System Preferences for Mission Control Moving left/right a space:

19-pty-fs8

You can choose whatever you want, as long as it consists of modifiers & letters (no F-keys, no arrows).


Here the macro. Just tested brievly, so it may contain bugs / room for improvement:

Move Frontmost Window to a Different Space [ver2.0].kmmacros (12.1 KB)


You have seen that there are two new hotkey triggers:

  • ⌃Left Arrow
  • ⌃Right arrow

They simply have the purpose to “pass through” the Space change (without windows movement). You could also say they reestablish the “old” arrow MC shortcuts, which had to be replaced by clunky non-arrow shortcuts in order to make them work with the macro.

The new condition (the second action, green in the screenshot) has the purpose to filter window movements against mere Space changes (with ⌥-key: window movement to the next Space; without ⌥-key: just switch to the next Space, without dragging along the window).
Technically: Without ⌥-key the mouse is not clicked-and-hold while performing the Space switch.

The direct Space change actions (⌃⌥1 etc.) should be unaffected by all this, and you don’t have to change those shortcuts in the System Prefs (as long as they are ⌃1, ⌃2, etc., of course)

PS:

Thanks again, @peternlewis !

2 Likes

Thank you very much for updating your macro :clap::+1:
I kept the previous version (disable). Maybe Apple still has pity and fixes the bug in the next macOS update.

Not sure if this, from Apple’s point of view, is a bug. May well be that it (the interdiction to script system shortcuts that contain other things than letters/numbers) is a security feature (from Apple’s point of view, again) :star_struck: :nauseated_face: :sweat:

1 Like

Something indeed seems to be buggy here:

All of a sudden my Mission Control shortcut for “Move left a space” stopped working, independently of the KM macro (KM engine off). The shortcut for “Move right” continued working.

Of course I tried it with different shortcut combos in the Keyboard Preferences, I shuffled the Spaces, I unplugged the external display, rebooted the Mac, did an SMC reset, an NVRAM reset.

What finally helped was a reboot in Safe Mode. But, interestingly, while in Safe Mode the Mission Control shortcut was still dysfunctional. Only after booting again in normal mode the shortcut was back to work. So, my only explanation is, it was some weird corruption in the system caches (which get cleared by the Safe Mode).

1 Like

@Tom Thank you so much for making this macro! I spent hours trying to figure out how to move a window to a different space.

Do you happen to know how to move just the window and go back to original workspace? For instance, if I'm on Desktop 3, I just want to move my Chrome app to Desktop 2 while staying on Desktop 3.

Hi Kenny,

I don’t think it is possible to move a window while staying on the current Space. However, in theory you could make the macro take you back to the original Space, after the window has been moved.

For this to work the macro has to know the current Space at macro launch. I think this is not really trivial.

The most convenient thing would be to get the current Space via AppleScript. But I don’t know if this is possible; I doubt it. (@ccstone ?)

You can get the current Space from UserDefaults:

defaults read com.apple.spaces
Click here to see a (shortened) sample output of that command
alice:~ tom$ defaults read com.apple.spaces
{
    SpacesDisplayConfiguration =     {
        "Management Data" =         {
            Age = "29576.78084978485";
            Double =             {
                […]
]            };
            "Management Mode" = 1;
            Manager =             {
                […]
            };
            Monitors =             (
                                {
                    "Current Space" =                     {
                        ManagedSpaceID = 1;
                        id64 = 1;
                        type = 0;
                        uuid = "";
                        wsid = 1;
                    };
                    "Display Identifier" = Main;
                    Spaces =                     (
                                                {
                            ManagedSpaceID = 1;
                            id64 = 1;
                            type = 0;
                            uuid = "";
                            wsid = 1;
                        },
                                                {
                            ManagedSpaceID = 6;
                            id64 = 6;
                            type = 0;
                            uuid = "AB4ACAF6-1F1B-4400-8A8D-64DBC1238047";
                        },
                                                {
                            ManagedSpaceID = 7;
                            id64 = 7;
                            type = 0;
                            uuid = "CF6BCFEF-B85B-43F2-916D-2332A76E1F37";
                        }
                    );
                },
                                {
                    "Current Space" =                     {
                        ManagedSpaceID = 2289;
                        id64 = 2289;
                        type = 0;
                        uuid = "C041359B-E97C-44F9-B8BF-051FD70004ED";
                    };
                    "Display Identifier" = "F03B120E-3C33-328F-0325-8C9341661C14";
                    Spaces =                     (
                                                {
                            ManagedSpaceID = 2289;
                            id64 = 2289;
                            type = 0;
                            uuid = "C041359B-E97C-44F9-B8BF-051FD70004ED";
                        }
                    );
                }
            );
            Primary =             {
                […]
            };
            Secondary =             {
                […]
            };
            Single =             {
                […]
            };
            Tile =             {
                […]
            };
        };
        "Space Properties" =         (
                        {
                name = "";
                windows =                 (
                    168556,
                    163025,
                    162520,
                    168396,
                    126475,
                    168366,
                    168378,
                    168373,
                    419,
                    28326,
                    34250,
                    25645,
                    128,
                    134246,
                    84643,
                    99954,
                    84670,
                    78221,
                    42480,
                    177,
                    134,
                    92861,
                    37035,
                    29365,
                    41199,
                    124262,
                    102,
                    98325,
                    30851
                );
            },
                        {
                name = "AB4ACAF6-1F1B-4400-8A8D-64DBC1238047";
                windows =                 (
                    168556,
                    126475,
                    28326,
                    34250,
                    25645,
                    128,
                    134246,
                    134,
                    102,
                    98325,
                    30853
                );
            },
                        {
                name = "CF6BCFEF-B85B-43F2-916D-2332A76E1F37";
                windows =                 (
                    168556,
                    126475,
                    28326,
                    34250,
                    25645,
                    128,
                    134246,
                    134,
                    102,
                    98325,
                    159342,
                    139565,
                    126115,
                    146589,
                    116441,
                    149,
                    180,
                    148736,
                    117691,
                    30855
                );
            },
                        {
                name = "C041359B-E97C-44F9-B8BF-051FD70004ED";
                windows =                 (
                    29364,
                    168343,
                    168266
                );
            }
        );
    };
    "app-bindings" =     {
        […]
    };
    "spans-displays" = 0;
}

  • The “Current Space” is listed for each display (monitor) and can have one or more of several type of IDs: id64, ManagedSpaceID, uuid and wsid.
  • Furthermore “Space Properties” contains a list of all windows (by window id) that are part of that Space.

So, what you’d have to do in the macro is basically this:

  1. Extract the appropriate identifier for the current Space of your current display (or of the frontmost window) from the output of the command line, and save it to a variable.
  2. Move the window to a different Space.
  3. Go back to the original (saved) Space, either by…
    • scripting MissionControl (if possible)
    • simulating the keyboard shortcut for the original Space

If you move the window with the arrow keys (⌃⌥ or ⌃⌥), then it should be somewhat simpler:

  1. In the macro, count how many times you hit the arrow-key shortcut.
  2. At the end make the macro execute (or ) the appropriate number of times.

This should bring you back to the original Space. But this won’t work if you move the window with a number key (for example ⌃⌥3).


Maybe somebody has some better ideas…

2 Likes

Hey Tom,

Not so far as I'm aware. (I've got feelers out though.)

Thanks for this!

-Chris