Automating Spaces in Mission Control

I plug my M1 MacBook Pro into a docking station with two monitors. I have created Startup and Shutdown automations for when I connect and disconnect the laptop in order to setup the monitor screens. Is there a way to automate the creation of a new Space in Mission Control on each monitor? Each one has different apps in different Spaces, and disconnecting the laptop always results in the extra Spaces on the secondary monitor being merged with the primary for use on the laptop screen.

I have looked in Shortcuts and Keyboard Maestro. Am I missing something? I’ve been looking into Better Touch Tool. Does that have the automation? TIA.

This reminds me of Terpstra’s Bunch, and his use of Moom to save snapshots of window layouts.

Part of that is this AppleScript that tells Moom to arrange the windows after Bunch has opened them.

tell application "Moom" to arrange windows according to snapshot "snapshot name"

So it might be possible to use Keyboard Maestro to detect when you’ve connected external monitors, wired network, etc. and tell Moom to do its thing.
Then again, it might be easiest just to run a Bunch (or KM, etc.) with a couple of clicks.

Maybe this can help?

It is not clear to me if the issue is that when you disconnect from the docking station, all open windows collapse into one space on the laptop. Or that the windows do not go back to the correct spaces when you reconnect to the docking station. Perhaps it is both. Any suggestions would depend on which it is.

And is it related to or a continuation of this thread:

If so, it seems that window managers have not given the result you desire?

One thing you can try (and may already have, based on the prior thread) is to assign applications to specific spaces.

If you mean “Can I programmatically launch an app on a specific Space” the answer is no, unless something has changed pretty recently. I asked Brett this very question not too long ago.

But you can switch to a space and then launch an app or open a window.

I do this with Bunch.

You can also determine the default space an app launches in. I use moom in combination with Keyboard Maestro to control spaces. My keyboard maestro macros minimize all windows first and then launch the Moom layout. Works well…

Yes, that’s why I used the word “programatically”.

How do you do this?

Not sure what the distinction is, but ok.

Perhaps @95omega means this?

I use it to have apps launch in specific spaces (aka “desktops”).

In this case I meant it to mean “all in code” ie having it in one or more apps (programs).

Thanks for reminding me about that Spaces option, I’d completely forgotten about that.

1 Like

Another possible factor, if I’m reading your first post correctly, is whether you have your Spaces linked.

I have linked Spaces and when I disconnect my laptop, all the windows in Spaces 1-4 of my Studio Display move respectively to Spaces 1-4 on my laptop. When I plug it back in, they go back to the correct Space on the Studio Display. They even usually resume their larger-than-laptop size.

Except Microsoft Teams, which is… special. :roll_eyes:

1 Like

Wow! Thanks for all the posts! Let me clarify. I have a shutdown shortcut that closes out everything before I disconnect from the Dock. I also have a startup shortcut menu that I use to launch certain apps on certain monitors (external monitors 1 & 2) in certain Spaces.

The problem I am running into is that when I reconnect the laptop to the docking station, all but 1 of the Spaces from Monitor 2 have been merged to Monitor 1 (the main display). I then have to create 2 Spaces on Monitor 2 before I can launch the apps into them in my startup shortcut. I am trying to add a step in the shortcut (or some other automation platform) to create the needed Spaces on Monitor 2 first. If not in Shortcuts, then: Keyboard Maestro, Apple Script, Better Touch Tool, or something else?

Which, if any, of these check boxen do you have checked?

Screen Shot 2022-09-29 at 9.52.38 AM

  1. NEVER on
  2. Always on
  3. Always on
  4. N/A - using only one display (my MacBook Air) at the moment

NEVER EVER! :slight_smile:

2 Likes

Not sure if that fits your bill, but I made two Keyboard Maestro Macros for creating and deleting Spaces. Both are mere keyboard shortcuts for triggering an AppleScript that will use UI scripting to click on the add and close buttons. So they can be recreated in whatever tool you prefer, but… they’re not tidy to put it kindly.

When I previously researched this, creating spaces could be done easily via keyboard shortcut via Yabai. But it required disabling SIP, which was a no go for me. Hence this ugly UI AppleScript workaround.

Here’s the one for creating a new space

do shell script "open -b 'com.apple.exposelauncher'"
delay 0.2
tell application id "com.apple.systemevents"
	tell (every application process ¬
		whose bundle identifier = "com.apple.dock") to ¬
		click (button 1 of group 2 of group 1 of group 1)
	delay 0.2
	key code 53 -- esc key
end tell

There’s the one for closing the current space

tell application "Mission Control" to launch
delay 0.25
tell application "System Events"
	tell list 1 of group 2 of group 1 of group 1 of process "Dock"
		set countSpaces to count of buttons
		if countSpaces is greater than 1 then
			perform action "AXRemoveDesktop" of button countSpaces
		end if
	end tell
	delay 0.6
	key code 53 --  # Esc key on US English Keyboard
end tell
2 Likes

@MevetS Here are my settings:

  • No
  • No
  • Yes
  • Yes

Thanks.

What is the reason you have the “Displays have separate Spaces” checkbox checked? That is, is this required for your workflow? If not, try unchecking it. Note that this is just a guess.

And to make sure I understand, is it that the spaces do not exist on the second monitor? Or is it that they exist but are ignored, with all apps and windows opening on the first monitor?

If it is the latter, you might also take a look at this app, SizeUp. And see this page which shows how to use the app to send a window to a monitor or space using AppleScript. And since it is scriptable it can be automated. Note that I have not used this app.

If it is the former I know of no way to automate the creation of a space.

@mevets When the laptop is docked, I have 3-4 Spaces on Monitor 1 (the principal one) and 2-3 Spaces on Monitor 2, each having certain apps configured to open on those monitors in those Spaces. When I disconnect from the dock and then reconnect, I will have 5-6 Space on Monitor 1 and only 1 on Monitor 2.

I need to automate the creation of Spaces on Monitor 2. I can always manually delete the extra Spaces on Monitor 1 (unless there’s a way to automate that).

I forget why I selected “Displays have separate Spaces.” I suspect I did so to solve an earlier problem.

Curious. My spaces are persistent across shutdown and startup as well as displays connecting and disconnecting. But I do not segregate spaces by displays. As an experiment you could uncheck that option and see if that changes things (and if so hopefully for the better).

I’ve not been able to find any mechanism to automate the creation of spaces, other than the AppleScript ldebritto has shared above.