我正在尝试创建一个在某个时间递增的计时器(在这种情况下,每当时间是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
答案 0 :(得分:0)
我认为您的代码会对您造成一些影响。
首先要注意的是,每个空闲事件只会调用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
脚本时,您将获得一个显示“hello”的对话框,然后您将获得“空闲测试”!消息多次,直到你退出脚本。 请记得将其保存为“保持开放状态”
要解决此问题,如果您希望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
处理程序,而不是在没有它的情况下在脚本中编写代码。
答案 1 :(得分: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
或者,您可以通过专为此类任务设计的AppleScript-ObjC桥使用NSTimer
,或设置launchd
脚本以在指定时间运行AppleScript不希望被捆绑到一个逗留开放的小程序。