Move Frontmost Window to a Different Space

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

Dear community,
This macro continues to work on MacOS Ventura? I'm unable to get it to work...

Thanks

BS

I don't have Ventura. Can just say that on Monterey (12.6.1 ) it still works.

Make sure that the Mission Control shortcuts used in the macro actions are in sync with your real Mission Control shortcuts (System Preferences > Keyboard > Shortcuts > Mission Control).

Also the hotkeys of the macro shouldn't be the same as the Mission Control shortcuts. (Though it's OK if they "overlap" in terms of modifier keys, like 3 as a macro hotkey and 3 as Mission Control shortcut.)

Hi Tom,
It seems to be related with a external display I've connected to macbook. It keeps saying that it doesn't exist.

So, with the internal display (without having connected any other), it does work in Ventura?

Yes it works fine.

Thanks for the info; I have upgraded to Ventura now too and can confirm. I have not yet investigated in issues with external displays though.

(Actually I do have a second display to my iMac, but last time I used it was 6 months or so ago and currently the cable is missing for some obscure reasons…)