0

特定の時間 (この場合、時間が xx:03:24 になるたびに) にインクリメントし、ユーザーが入力した値に達したときに通知を送信するタイマーを作成しようとしています。完了すると、インクリメントがリセットされ、手動で終了するまで繰り返されます。

ただし、このコードは、想定されているときに確実にトリガーされません。誰かが理由について推測していますか?

on quit
    continue quit
end quit
tell application "Notifications Scripting"
    set event handlers script path to (path to me)
end tell

global actions, newActions

using terms from application "Notifications Scripting"
    on notification activated
        tell application "Safari"
            activate
            set winlist to every window
            repeat with win in winlist
                set numtabs to 0
                try
                    set tablist to every tab of win
                    set numtabs to length of tablist
                end try
                if numtabs is greater than 0 then
                    repeat with t in tablist

                        if "fallenlondon.storynexus.com" is in (URL of t as string) then
                            tell application "System Events"
                                tell window id (id of win as integer) of application "Safari" to set current tab to tab (index of t as integer)
                            end tell
                        end if
                    end repeat
                end if
            end repeat
        end tell
    end notification activated
end using terms from

display dialog "How many actions do you want to build up before being notified?" default answer "1"

set actions to (text returned of result) as number
set newActions to 0

on idle
    if ((seconds of (current date)) = 24) and ((minutes of (current date)) mod 10 = 3) then
        set newActions to (newActions + 1)
        set delayTime to 540
    else
        set delayTime to 0.5
    end if

    if newActions ≥ actions then
        if newActions = 1 then
            tell application "Notifications Scripting" to display notification "Fallen London" message "You have " & newActions & " new action!" sound name "Glass"
        else
            tell application "Notifications Scripting" to display notification "Fallen London" message "You have " & newActions & " new actions!" sound name "Glass"
        end if
        set newActions to 0
    end if
    return delayTime
end idle
4

2 に答える 2

0

あなたのidle実装が確実に機能することは期待できません。これは正確なタイミング メカニズムではありません。したがって、次の仮定は適切ではありません。

if ((seconds of (current date)) = 24) and ((minutes of (current date)) mod 10 = 3) then

ここでは、10 分間のループで 1 秒のウィンドウをヒットしようとしています。そのウィンドウを逃した場合は、再試行するまでさらに 10 分かかります。

それを行う信頼できる方法は、次のトリガーの時刻を表す日付を保存し、現在の日付がその日付より後であるかどうかを定期的に確認し、そうあればトリガーすることです。

to nextTriggerDate() -- returns next xx:x3:23 date
    set d to current date
    set {d's minutes, d's seconds} to {((d's minutes) div 10 * 10) + 3, 23}
    if d < (current date) then set d to d + 10 * minutes
    d
end nextTriggerDate

property _triggerDate : nextTriggerDate()


on idle
    if (current date) > _triggerDate then
        set _triggerDate to nextTriggerDate()
        -- DO STUFF HERE...
    end if
    return 1
end idle

NSTimerまたは、このタイプのタスク専用に設計された AppleScript-ObjC ブリッジを介して使用するかlaunchd、開いたままのアプレットに縛られたくない場合は、指定された時間に AppleScript を実行するようにスクリプトを設定することもできます。 .

于 2016-07-24T21:44:07.830 に答える