Automating organisation of multiple screens and desktops

I have a regular monthly task where I set up different docs and apps on different desktops on a MacBook Pro and separate monitor. I use 6 desktops in total. I have not been able to automate it completely.

I can see how Keyboard Maestro can set up on apps and windows on different desktops on the Mac but I have not been able to find a way to nominate specific desktops on both the MBP and the monitor. AppleScript maybe? - but I haven’t found anything yet.

Any ideas?

Just to clarify: I am assuming that when you say Desktops, you are referring to the MacOS feature of creating multiple virtual desktops on a given display and then switching between them, since you are using 6 desktops across two displays, the built in display on your MBP and the external monitor, correct?

Assuming this to be the case, I do not believe there is a way to accomplish this task. I have periodically surfed the web looking for a way to programmatically assign windows or apps to the virtual desktops, but have never found a way to do that. As far as I can determine, however Finder (or window server) does this is not documented or publicly available.

You might think that you could create, in System Preferences, set shortcut keys for each Desktop (eg control-1, control-2, etc) which I have done, and then use AppleScript eg tell application "System Events" to keystroke "2" using control down to inject that keystroke to go to desktop 2, but this does not work.

In terms of just opening windows on specified displays, that is doable, because Keyboard Maestro does have built-in actions for moving windows between displays. On your MBP, display 0 is going to be your built in display and display 1 will be the external monitor, and it is easy to move windows to the appropriate display and resize and position them with KM’s actions.

How to get them to the proper virtual desktops remains a mystery, at least to me and to everything else I have ready online.

I wonder: Do apps like Moom handle virtual desktops? If so, then there is a programmatic interface in one of the FrameWorks, although I have not found that in (limited) searching in Xcode documentation, and you could use Moom (or a similar app) with a remembered window layout for this purpose.

Looks like “no”:

Moom Support says

Unfortunately, Moom cannot be used to send windows to other spaces (desktops). At present, Apple doesn’t have a public programming interface that we can legally use to do that. If that situation ever changes, we will add such capability to Moom.

Thanks. Yes, your assumptions about desktops/spaces is correct.

Many thanks for your comments.

I can’t see a way to make Moom do it either.

Ah well.

I think you could do this with a combination of Mission Control and Workspaces.
Workspaces opens sets of documents and applications that you define.
Mission Control can be used to assign applications to specific Desktops.
The combination of the two might work for you.
Unfortunately, this seems to be a one-off thing, where you assign the app to a Desktop, or you don’t. A thought of a workaround is creating another user on macOS with their apps assigned to Desktops. At some point the problem becomes easier than the workaround :slightly_smiling_face:

Edit: something else that might be relevant (or not even work anymore):

I had thought of assigning the apps in question to specific desktops, but thought that was too restrictive unless you would always want those apps on those desktops.

I do think I have a solution for you that will accomplish what I think you want, which is to be able to have a group of apps on screen and easily switch to another group and so forth.

Let’s, for the sake of illustration, say you have 9 apps, labelled A thru I, and you want them to appear on 3 desktops, with A, B, C on desktop1, D, E, F on Desktop 2, and G, H, I on desktop 3. For illustration I am also assuming only one display, but this easily generalizes to your second display.

You could write an AppleScript or a KM Macro that shows apps A, B, C and hides apps, D, E, F, G, H, I. A second script shows D, E, F and hides A, B, C, G, H, I, and so forth. The result is that with execution of the proper script, the apps you want on the “desktop” are shown and the ones that would be on a different desktop are hidden. You are using only one desktop but using automation to rapidly shift between the visible and hidden apps.

You could probably implement this with Moom which I believe has a feature to allow it to “memorize” and recall window configurations.

Since I don’t use Moom, I would do this with KM; depending on KM’s action library and how well it fit with this task I might use AppleScript actions in KM or not.

If I were to implement this (I might since this might be a better way than my current use of desktops), I would have a single KM macro that had multiple hot key triggers. In that macro I would retrieve the trigger so I would know how it was invoked and therefore which apps should be display and which hidden.

I would have a table, one row per app, with columns:

  • App name
  • “Desktop” the app should appear on (numbered 0, 1, …)
  • Display the app should appear on (numbered 0 for your laptop display and 1 for the external display)

Another table correlates the triggers with the desktop number.

The macro gets the trigger, uses the table to find out which desktop is to be visible. Then iterate though the apps. If the desktop matches that in that table row, show the app on the designated display. Otherwise, hide it.

This would allow you to quickly and easily add/remove apps from this automation or move them from one desktop to another.

If you wanted you could add a column to the table for the app window’s location, but that starts to become very convoluted if the app has multiple windows open, so I probably would not do that. You could easily have another macro that found the current window coordinates for all windows for all the apps, wrote this data to a KM variable or a file, and a macro to restore things. (I guess at this point we might as well move to Xcode and write our own version of Moom…!).

This might allow you to accomplish what you are looking to do without being able to control the desktops themselves.

I don’t know if you can use AppleScript to set desktop backgrounds. Since desktops can have different backgrounds, that would be useful if you use different backgrounds to differentiate your desktops (I actually use a neutral gray on the desktop where I do photo editing but the standard MacOS background on all others).

Thanks JohnAlt, yeah, tried that too. One issue is an inconsistency in MacOS: if I right-click on the app icon in the dock, look under option, sometimes it allows me to nominate a specific desktop on Display 1 or 2. But only sometimes! And even then it gets the Desktop number wrong e.g. At the moment Bear is on Desktop 8 on Display 2. But right-clicking on the app in the dock and looking under Options shows it as Desktop 7.

@Tiff_Sunbacon As a thought, make sure in System Preferences you do not have the desktop order being reordered based on most recent use. There is a checkbox for that although I am not sure offhand which preference panel it is in.