Automator + Applescript 如何:新桌面(在其中打开日历和提醒)

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

标题是我想要实现的。

在 Automator 中,我试图简单地记录打开新桌面的操作,然后打开其中的应用程序 -- 但我得到

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

此外,如果我这样做,记录的动作是"Click the Desktop 4 button"。

同样,我在谷歌上搜索并找到了创建特定桌面(例如桌面 3)的 Applescripts,但我总是打开不同数量的桌面。所以我希望工作流简单地制作一个 new 桌面,而不管我已经打开了多少。此外,我发现的许多 Applescripts 都是针对 Mavericks 的,它们仍然有 Spaces 而我有 Yosemite。

我知道如何让脚本打开日历和提醒,所以主要问题是如何打开它或创建新桌面。

花了一段时间,但我想到了这个。适用于 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

在 macOS Mojave (10.14.3) 上运行良好

AppleScript:

tell application "System Events"
    tell application "Mission Control" to launch
    tell group 2 of group 1 of group 1 of process "Dock"
        click (every button whose value of attribute "AXDescription" is "add desktop")
        tell list 1
            set countSpaces to count of buttons
            delay 0.5
            click button (countSpaces)
        end tell
    end tell

    delay 0.5
    tell application "Calendar" to launch
    tell application "Reminders" to launch
end tell

JXA:

Application("Mission Control").launch()

var proc = Application("System Events").processes['Dock']
var group = proc.groups[0].groups[0].groups[1]

var bs = group.buttons.whose({ description: "add desktop"})
Application("System Events").click(bs[0])

delay(0.5)
var li = group.lists[0]
Application("System Events").click(li.buttons[li.buttons.length - 1])

delay(0.5)
Application("Calendar").activate()
Application("Reminders").activate()

这适用于我的 macOS Mojave 10.14.4

如果您使用其他语言,您需要将 "add desktop" 替换为您的系统语言。

AppleScript:

tell application "System Events"
tell application "Mission Control" to launch
tell group 2 of group 1 of group 1 of process "Dock"
    click (every button whose value of attribute "AXDescription" is "添加桌面")
    tell list 1
        set countSpaces to count of buttons
        delay 0.5
        click button (countSpaces)
    end tell
end tell

macOS 蒙特雷 12:

tell application "System Events"
    --mission control starten
    tell application "Mission Control" to launch
    delay 0.25
    tell process "Dock"
        set countSpaces to count buttons of list 1 of group 2 of group 1 of group 1
        --new space
        click button 1 of group 2 of group 1 of group 1
        --switch to new space
        repeat until (count buttons of list 1 of group 2 of group 1 of group 1) = (countSpaces + 1)
        end repeat
        click button (countSpaces + 1) of list 1 of group 2 of group 1 of group 1
    end tell
    delay 0.25
    tell application "Calendar" to launch
    tell application "Reminders" to launch
end tell