Using applescript to create spaces and launch apps

Solution 1:

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.

Solution 2:

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'"