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

7

我想要实现的是标题中所述的目标。

在 Automator 中,我尝试简单地记录打开新桌面的操作,然后在其中打开应用程序 —— 但是我遇到了以下错误:

“Watch Me Do” 操作遇到错误。请检查该操作的属性,然后尝试再次运行工作流程

此外,如果我按照这种方式操作,则记录的动作是“单击桌面4按钮”。

同样,我搜索并找到了创建特定桌面(例如桌面3)的 AppleScripts,但我总是有不同数量的桌面处于打开状态。因此,我希望工作流程能够仅仅创建一个 新的 桌面,而不管我已经打开了多少个桌面。此外,我发现许多我找到的 AppleScripts 都是针对 Mavericks 的,它仍然具有 Spaces 功能,而我使用的是 Yosemite。

我可以弄清楚如何使脚本打开日历和提醒事项,所以主要问题是如何打开或创建新的桌面。

4个回答

11

我花了一些时间,但最终搞定了这个问题。此方法适用于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

1
太棒了!这个解决方案完美地解决了我的问题,非常感谢!:) 我想点赞你的解决方案,但我还没有足够的声望,所以等我有了再来点赞! - lwuun
这个解决方案似乎在macOS Sierra上不再起作用 :-( - WiRa

3

在 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()

我会+1;但是,由于当前的编码方式,不能保证一定会进入刚创建的桌面。换句话说,例如,如果我有2个桌面和一个应用程序在运行全屏时运行代码,则确实会创建第三个桌面;但是,我会进入全屏应用程序,而不是第三个桌面。此外,假设日历和提醒在运行时已关闭,则它们将在焦点所在的桌面上启动,而不是刚创建的桌面。您的代码还没有考虑到如果日历和/或提醒在运行代码时已经打开的情况。 - user3439894

1
这在我的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

将来请避免在您的答案中提出问题。如果您有另一个问题,请使用页面右上角的[提问]按钮发布它。 - Artemis
你显然只是从Mike Miklin的回答中复制了代码,删除了最后四行,并将“add desktop”改为了“添加桌面”,这样并没有真正回答所问的问题!并且在这样做的过程中,你复制的代码因为删除了一个必要的行,即一个结束end tell而无法编译。 - user3439894

0

macOS Monterey 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接