Search code examples
macosapplescriptdesktopautomator

Automator + Applescript how to: new Desktop (open Calendar and Reminders in it)


The title is what I'm trying to achieve.

In Automator, I've tried to simply record the actions of opening a new desktop, and then opening the apps in it -- but I get the

The action 'Watch Me Do' encountered an error. Check the actionʼs properties and try running the workflow again

Furthermore, if I do it in this way, the action that is recorded is "Click the Desktop 4 button".

Similarly, I googled and found Applescripts that create specific Desktops (e.g. Desktop 3) but I always have a different amount of Desktops open. So I want the Workflow to simply make a new Desktop regardless of the amount I have open already. Moreover, many of the Applescripts I do find are geared towards Mavericks which still had Spaces and I've got Yosemite.

I can figure out how to make the script open Calendar and Reminders, so the main issue is how to have it open or create a new Desktop.


Solution

  • Took a while but I came up with this. Works on Mavericks.

    on run {input, parameters}
        my openNewSpace()
        my launchApplication("Reminders")
        my launchApplication("Calendar")
    end run
    
    on openNewSpace()
        tell application "System Events"
            --mission control starten
            do shell script "/Applications/Mission\\ Control.app/Contents/MacOS/Mission\\ Control"
            tell process "Dock"
                set countSpaces to count buttons of list 1 of group 1
                --new space
                click button 1 of group 1
                --switch to new space
                repeat until (count buttons of list 1 of group 1) = (countSpaces + 1)
                end repeat
                click button (countSpaces + 1) of list 1 of group 1
            end tell
        end tell
    end openNewSpace
    
    on launchApplication(app_name)
        tell application app_name
            launch
        end tell
    end launchApplication