Using applescript to create spaces and launch apps

applescriptautomationautomatorspaces

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.

Best Answer

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.