gpt4 book ai didi

applescript - 如何在特定时间启动 applescript

转载 作者:行者123 更新时间:2023-12-04 03:16:34 27 4
gpt4 key购买 nike

我正在尝试制作一个将在后台运行并且每周只在特定时间执行一次的 applescript,有什么建议吗?

最佳答案

您可能想要的是 idle handler .首次打开 AppleScript 应用程序时调用空闲处理程序(在它完成其“运行”处理程序后,如果有的话,则立即调用)。

空闲处理程序返回的任何数字都是空闲处理程序下一次运行的秒数;也就是说,OS X 将获取该数字,然后等待那么多秒,然后再次调用空闲处理程序。

这在实践中如何运作取决于您需要“特定时间”的精确度。例如,您可以只让它返回 7*24*60*60,每次空闲处理程序运行时,它也会在整整一周内再次运行。

或者,您可以在每次空闲处理程序运行时检查当前日期(包括当前时间),并让空闲处理程序返回 24*60*60,从而每天检查自己;或 60*60,每小时检查一次;或 60,每分钟检查一次。

这是一个非常简单的空闲处理程序:它在星期五的某个时候显示一个烦人的“你好”:

on idle
-- if today is Friday, say something!
set currentTime to current date
if the weekday of currentTime is Friday then
display dialog "Hello"
end if

--only check once a day
return 24 * 60 * 60
end idle

这将专门检查周五下午 5:00;它会每分钟检查一次当前时间:

on idle
-- if today is Friday between 5 PM and 5:01 PM, say something!
set currentTime to current date
set fivePM to 17 * 60 * 60
if the weekday of currentTime is Friday then
if the time of currentTime is greater than fivePM and the time of currentTime is less than (fivePM + 60) then
display dialog "Hello"
end if
end if

--only check once a minute
return 60
end idle

根据您的需要,您可能希望考虑使用一个属性来存储脚本最后一次执行它应该执行的操作的时间;属性跨运行时维护,可用于防止电源故障后或超过夏令时边界后的双重执行。

关于applescript - 如何在特定时间启动 applescript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25334780/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com