4

So the end goal of what I am looking to do is this:

When I am at work I use an external monitor with my MBP. I have a consistent set of spaces and apps that I setup everyday. What I would like to do is create an applescript or combination of applescript & automator application that once launched will create the needed number of spaces on each monitor and open then the appropriate apps into each space.

I don't know if this is best accomplished completely in applescript or as I said using a combination of applescript to create the spaces and automator to open the bulk of the applications and browser windows that I use.

2
  • not an answer, but there's a wealth of Applescript/Spaces routines at macscripter.net/viewtopic.php?id=23453
    – Tetsujin
    Mar 29, 2015 at 14:23
  • @Tetsujin, IIRC Those scripts do not work since OS X 10.8. FWIW Spaces and or Mission Control are not really script friendly, it a really hit or miss depending on what one is trying to do. Dec 16, 2017 at 14:04

2 Answers 2

4

To add one Desktop Space to each monitor/display:

do shell script "/Applications/Mission\\ Control.app/Contents/MacOS/Mission\\ Control"
delay 0.5
tell application "System Events" to click (every button whose value of attribute "AXDescription" is "add desktop") of group 1 of process "Dock"
delay 0.5
do shell script "/Applications/Mission\\ Control.app/Contents/MacOS/Mission\\ Control 1"

One can modify the code to add more desktops.

To switch to a particular desktop / fullscreen app in a dual display setting, e.g. Desktop 3:

do shell script "/Applications/Mission\\ Control.app/Contents/MacOS/Mission\\ Control"
delay 0.5
try
    tell application "System Events" to click (first button whose value of attribute "AXDescription" is "exit to Desktop 3") of list 1 of group 1 of process "Dock"
on error
    tell application "System Events" to click (first button whose value of attribute "AXDescription" is "exit to Desktop 3") of list 2 of group 1 of process "Dock"

end try

There are discussions out there about turning an app to fullscreen mode with script.

1

Click ‘add desktop’ button in the UI element "Spaces Bar" on Big Sur (not sure when this changed).

do shell script "open -a 'Mission Control'"
delay 0.5
tell application "System Events" to ¬
    click (every button whose value of attribute "AXDescription" is "add desktop") ¬
        of UI element "Spaces Bar" of UI element 1 of group 1 of process "Dock"
delay 0.5
do shell script "open -a 'Mission Control'"
3
  • Now I want to delete a space. I have read through n8henrie.com/2013/03/a-strategy-for-ui-scripting-in-applescript and apple.stackexchange.com/questions/40436/… . I feel confident that I can use this in combination with what you have taught me to find out how to click the "X" button for the space that I'm currently in. I'll add to this answer when I figure out how to do that. I will need to use the description instead of the group number to accomplish this. Can't figure out how to get proper names yet. Jul 13, 2021 at 5:20
  • This should probably be moved to chat... but I'm trying to use what I've learned to traverse the UI element tree & meeting no success. Syntax error: Can’t get "exit to Desktop 3" of UI element 1 of UI element "Spaces Bar" of UI element 1 of group 1. Access not allowed. ... I'm pretty sure I'm in the right place. I'm one level deeper in the Accessibility Inspector, and I've tried a bunch of different things. Quite possible I just haven't tried the right thing yet, also possible that the "Access not allowed" statement means what it says. Will keep trying before asking for more help. Jul 13, 2021 at 5:39
  • This is just an aesthetic thing, but you can avoid "do shell script" with the more AppleScript-native tell application "Mission Control" to launch. I think this also lets you eliminate the delay reliably since your script won't get control back until the event has been responded to, whereas the shell script returns immediately.
    – Glyph
    Aug 16 at 17:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .