Automating Spaces in Mission Control

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.

@mevets I don’t think unchecking that box would help. This is what I found on the O’Reilly site:

" If ‘Displays have separate Spaces’ is turned on in the Mission Control panel of System Preferences then each monitor has its own set of Spaces. When you use one of the gestures or keystrokes for switching Spaces, you affect the Spaces on only one monitor: the one that contains your cursor at the moment."

Left unchecked, the Spaces would change on both monitors whenever I hit Ctl-Left or Ctl-Right.

Thanks for the posts, though! I guess there isn’t an automation solution until Apple fixes this.

Yep. It seems you need separate spaces per monitor for your workflow. One of my hobbies is photography, so when using an app like Photo Mechanic I’ll have windows open on multiple monitors, and I want all of the monitors to be one space. That’s what works for me. :slight_smile:

I was suggesting it just to see if it changed the behavior of what you are seeing, and if the issue was related to using that option, as that is a difference, albeit not the only one, in how we both use spaces.

You are welcome. I was an interesting challenge. And it sure would be useful to automate the addition and subtraction of spaces based on context.

Wow found this randomly using Google search, exactly what I was looking for thanks!

I have this problem too, but I have only one external display for the machine that gets regularly docked/undocked. I never found a solution and in the end decided that prior to undocking, to just move the spaces from the secondary (laptop) display over to the primary (external) display and tuen undock (and then move them back when I re-dock). It’s not elegant but it works and I don’t undock more than once a day or so.

For context, I arrange my spaces by project rather than application, so any app may have windows open on multiple spaces.

Edit: I just realized that this is an older post. Thanks @RodriguezLucha for resurfacing it, because I found new things to maybe try :slight_smile: Also, welcome to the forum!