如何在没有对话框的情况下暂停并恢复AppleScript?

如何在没有对话框的情况下暂停并恢复AppleScript?,applescript,Applescript,我正在用AppleScript概述一个工作流。该脚本从Omnifocus接收我需要执行的下一个任务,并要求我确定是否可以在2分钟内完成。如果可以的话,它会启动一个计时器,我希望它等到我真正完成任务。现在我有一个对话框弹出,我可以标记任务完成时,我与它完成。不幸的是,我需要做的一些任务是在Omnifocus中,在打开对话框的情况下,我不能在Omnifocus中做任何事情 我希望避免使用对话框,以便在脚本运行时可以在Omnifocus中工作。我希望能够告诉脚本我完成了,这样它就可以停止计时器,告诉我

我正在用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

提前感谢您的帮助。

我的机器上没有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
全局开始时间、结束时间、树列表、当前任务索引、任务名称、注释名称
在逃
告诉应用程序“OmniFocus”
告诉默认文档以告诉前文档窗口
将透视图名称设置为“每日总结”
告诉要将树列表设置为的内容(第一个叶的值)
结束
结束
将当前任务索引设置为1
beginTask()
结束
重新开放
--插入的try块有助于调试
尝试
将结束时间设置为(当前日期)
将经过的时间设置为结束时间-开始时间
说“那花了你”&用了多少时间和“几秒钟才完成”
显示对话框ProjectName&TaskName&NoteName按钮{“完成”、“延迟”、“取消”}默认按钮“完成”
设置按钮返回到返回结果的按钮
如果按钮返回=“完成”,则
标记完整的列表项
告诉应用程序“OmniFocus”
压缩默认文档
结束语
否则,如果按钮返回=“延迟”,则
显示对话框“延迟多长时间(分钟)?”默认回答“60”
将TimeAdd设置为返回的结果文本
将ListItem的延迟日期设置为((当前日期)+(TimeAdd*分钟))
告诉应用程序“OmniFocus”
压缩默认文档
结束语
否则,如果按钮返回=“取消”,则
告诉应用程序“OmniFocus”
压缩默认文档
结束语
否则,如果按钮返回=“否”,则
告诉应用程序“OmniFocus”
压缩默认文档
结束语
如果结束
否则,如果按钮返回=“否”,则
显示对话框“分解任务”
将透视图名称设置为“项目”
如果结束
将当前任务索引设置为当前任务索引+1

如果当前任务索引常规AppleScript无法真正执行,但AppleScriptObjC可用于创建显示窗口或在菜单栏中放置状态项以显示经过的时间。计时器只是一种你手动启动/停止的东西吗?是的。这只是为了让我负责。我试着把任务分解成小块。计时器会给我反馈,以确保我成功并专注于我应该做的事情。我不使用OmniFocus,但看看你的脚本,它看起来非常适合编写脚本。由于这听起来像是供个人使用的东西,只有在你专注于完成这些任务时才会运行,因此创建一个在后台空闲的保持打开的应用程序并每隔几秒钟进行一次轮询,以检查你的项目列表中的某个特定任务是否在OmniFocus中被勾选为已完成,这并不是不合理的。可能比对话框更好?您可以使用保持打开的脚本应用程序来实现这一点,即使使用香草AppleScript,但是如果您需要铃铛和口哨,您需要转到AppleScriptToBjc。今天晚些时候我将发布一个基本示例。这是一个重大进展。非常感谢。我已经能够调试脚本并创建应用程序。在告诉我花费了多长时间后,它不会创建对话框。我无法在脚本中标记任务已完成。我想我可以排除故障,但我不知道如何查看应用程序的日志。我只能在编辑器中看到脚本第一部分的日志。我无法在编辑器中“重新打开”脚本。这意味着当应用程序运行时,我看不到哪里出了问题。“必须有办法得到日志——我只是不知道它是什么。”威尔麦克莱莫尔——首先,我想我看到了问题所在。我忘了在开始时将
ProjectName
添加到全局变量列表中,因此您可能在
重新打开
处理程序中的display对话框语句中遇到“unassigned variable”错误。哎呀!不过,更一般地说,您可以将整个代码块封装在try博客中,并通过应用程序显示错误。我将编辑上面的
重新打开
处理程序,以便您了解我的意思;看到后告诉我,这样我就可以把代码恢复正常。泰德,我正在路上。谢谢你的帮助。我不清楚try块如何为我提供更多关于错误的信息,但如何根据需要修复定义的变量。“重新打开”代码回答了我的问题,并做了我希望它做的事情。非常感谢。