首页 > 解决方案 > 如何在没有对话框的情况下暂停和恢复 AppleScript?

问题描述

我正在努力在 AppleScript 中概述工作流程。该脚本需要我从 Omnifocus 执行的下一个任务,并要求我确定是否可以在 2 分钟或更短的时间内完成。如果可以的话,它会启动一个计时器,我希望它等到我真正完成任务。现在我会弹出一个对话框,我可以在完成后将任务标记为完成。不幸的是,我需要在 Omnifocus 中完成一些任务,而在对话框打开的情况下,我无法在 Omnifocus 中执行任何操作。

我想避免使用该对话框,以便在脚本运行时可以在 Omnifocus 中工作。我希望能够告诉脚本我已完成,以便它可以停止计时器,告诉我完成任务需要多长时间,然后继续在 Omnifocus 中将任务标记为完成。我最初认为最好的方法是输入一个组合键。经过一番研究,我认为我不能在 AppleScript 中做到这一点。我对如何允许我在脚本中间工作然后告诉程序我完成任务的任何想法持开放态度。

这是我的代码:

on run {}
    with timeout of (30 * 60) seconds
        tell application "OmniFocus"
            activate
        end tell
        tell application "OmniFocus"
            tell default document to tell front document window
                set perspective name to "Daily Wrap-Up"
                tell content to set TreeList to (value of first leaf)
                repeat with ListItem in TreeList
                    set ProjectName to name of containing project of ListItem as text
                    set TaskName to " - " & name of ListItem
                    set NoteName to " - " & note of ListItem
                    display dialog "The task is:" & return & ProjectName & TaskName & NoteName & return & "Can you do this in 2 minutes or less?" buttons {"Yes", "No"} default button "Yes"
                    set Button_Returned to button returned of result
                    if Button_Returned = "Yes" then

                        say "Get to work!"


                        set T1 to minutes of (current date)

                        set T1s to seconds of (current date)


                        display dialog "Click when done." buttons {"Complete", "Cancel"} default button "Complete"
                        set Button_Returned to button returned of result

                        if Button_Returned = "Complete" then



                            set T2 to minutes of (current date)

                            set T2s to seconds of (current date)

                            set TT_ to ((T2 * 60) + T2s) - ((T1 * 60) + T1s)

                            say "that took you" & TT_ & "seconds to complete"
                            display dialog ProjectName & TaskName & NoteName buttons {"Complete", "Defer", "Cancel"} default button "Complete"
                            set Button_Returned to button returned of result
                            if Button_Returned = "Complete" then
                                mark complete ListItem
                                tell application "OmniFocus"
                                    compact default document
                                end tell
                            else if Button_Returned = "Defer" then
                                display dialog "Defer for how long (in minutes)?" default answer "60"
                                set TimeAdd to text returned of result
                                set defer date of ListItem to ((current date) + (TimeAdd * minutes))
                                tell application "OmniFocus"
                                    compact default document
                                end tell
                            else if Button_Returned = "Cancel" then
                                tell application "OmniFocus"
                                    compact default document
                                end tell
                            else if Button_Returned = "No" then
                                tell application "OmniFocus"
                                    compact default document
                                end tell
                            end if
                        else if Button_Returned = "No" then
                            display dialog "Breakdown task."

                            set perspective name to "Projects"


                        end if
                    end if
                end repeat
            end tell
        end tell
    end timeout
end run

提前感谢您的帮助。

标签: applescript

解决方案


我的机器上没有 OmniFocus,所以我不能正确地编译这个更少的测试,但是在 vanilla AppleScript 中你可以执行以下操作:

global start_time, end_time, TreeList, current_task_index, TaskName, NoteName

on run
    tell application "OmniFocus"
        tell default document to tell front document window
            set perspective name to "Daily Wrap-Up"
            tell content to set TreeList to (value of first leaf)
        end 
    end
    set current_task_index to 1
    beginTask()
end

on reopen
    -- inserted try block to aid debugging
    try
        set end_time to (current date)
        set elapsed_time to end_time -start_time
        say "that took you " & elapsed_time & " seconds to complete"
        display dialog ProjectName & TaskName & NoteName buttons {"Complete", "Defer", "Cancel"} default button "Complete"
        set Button_Returned to button returned of result
            if Button_Returned = "Complete" then
                mark complete ListItem
                tell application "OmniFocus"
                    compact default document
                end tell
            else if Button_Returned = "Defer" then
                display dialog "Defer for how long (in minutes)?" default answer "60"
                set TimeAdd to text returned of result
                set defer date of ListItem to ((current date) + (TimeAdd * minutes))
                tell application "OmniFocus"
                    compact default document
                end tell
            else if Button_Returned = "Cancel" then
                tell application "OmniFocus"
                    compact default document
                end tell
            else if Button_Returned = "No" then
                tell application "OmniFocus"
                    compact default document
                end tell
            end if
        else if Button_Returned = "No" then
            display dialog "Breakdown task."
            set perspective name to "Projects"
        end if
        set current_task_index to current_task_index + 1
        if current_task_index <= count of TreeList then
            beginTask()
        else
            quit
        end
    on error errstr number errnum
        display alert "Error " & errnum & ": " & errstr
    end try
end

on idle
    (*
        you can use this handler if you want the app to give you a countdown, or 
        announce a time limit, or anything that needs doing while you're working on the task
    *)
end

on beginTask()
    tell application "OmniFocus"
        tell default document to tell front document window
            set perspective name to "Daily Wrap-Up"
            set ListItem to item current_task_index of TreeList
            set ProjectName to name of containing project of ListItem as text
            set TaskName to " - " & name of ListItem
            set NoteName to " - " & note of ListItem
            display dialog "The task is:" & return & ProjectName & TaskName & NoteName & return & "Can you do this in 2 minutes or less?" buttons {"Yes", "No"} default button "Yes"
            set Button_Returned to button returned of result
            if Button_Returned = "Yes" then
                say "Get to work!"
                set start_time to (current date)
            end if
        end tell
    end tell
end

将其复制到脚本编辑器中,对其进行调试,然后将其保存为应用程序,并选中“运行处理程序后保持打开”复选框。操作如下:

  1. 当 OmniFocus 准备就绪时,运行此脚本应用程序。它将提示您开始第一个任务。
  2. 完成第一个任务后,再次双击脚本应用程序图标以调用reopen处理程序。该脚本将向您显示第一个任务的已用时间,为您提供您概述的选择,然后提示您开始第二个任务。
  3. 最后一个任务完成后,脚本会自动退出。

开头的全局变量允许在脚本进行时在处理程序之间传递必要的数据。

如果您想要更复杂的东西——例如,为您提供更细粒度控制的浮动窗口或菜单栏项——那么您将需要开始使用 ASOC 来构建它。但这是微调;这应该给你一个一般的结构。


推荐阅读