我正在尝试创建一个计时器,它在某个时间(在这种情况下,只要时间是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
我不希望您的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
或者,您可以通过专门为此类任务设计的AppleScript ObjC桥使用NSTimer
,或者如果您不想绑定到保持打开的小程序,则可以设置launchd
脚本在指定时间运行AppleScript。
我认为您的代码有一些问题。
首先要注意的是,只有idle
处理程序中的代码才会在每个空闲事件上被调用。换句话说,如果您希望脚本的主体与每个空闲进程一起运行,那么它不会。
举个例子。。。
on run
display dialog "hello!" giving up after 3
end run
on idle
display dialog "Idle test!" giving up after 3
return 5
on idle
当您将上面的代码保存为stay open
脚本时,您会看到一个对话框,上面写着"你好",然后您会多次收到"Idle test!"消息,直到您退出脚本记住将其保存为"保持打开"
要解决此问题,如果希望idle
处理程序之外的所有代码都能运行,请从中创建一个自定义函数,从空闲处理程序中调用该函数。
on yourCustomFunction()
-- All the code you want to run prior when the idle function is called
end yourCustomFunction
on idle
yourCustomFunction()
-- any other additional code you want to run here
return 5
end idle
还有一件事我想指出。您不需要tell application "Notifications Scripting"
,只需display notification
即可,如下所示。
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
display notification "You have " & newActions & " new action!" with title "Fallen London" sound name "Glass"
else
display notification "You have " & newActions & " new actions!" with title "Fallen London" sound name "Glass"
end if
set newActions to 0
end if
return delayTime
end idle
最后,我建议添加一个on run
处理程序,而不是只在没有它的脚本中编写代码