如何每 5 秒执行一个子程序?
How to perform a subroutine every 5 seconds?
我有一个子程序来检查是否安装了磁盘,
我想知道如何让这个子程序总是每 5 秒 运行。
提前致谢!
on checkMyDiskIsMounted()
tell application "Finder"
activate
if exists disk "myDisk" then
--do anything
else
--do anything
end if
end tell
end checkMyDiskIsMounted
四个选项:
马特
建议的带延迟的重复循环
repeat
-- code
delay 5
end repeat
带有 idle
处理程序的(保持打开状态)小程序
on run
-- do intialiations
end run
on idle
-- code
return 5
end idle
像 red_menace
建议的 AppleScriptObjC 通知
观察 /Volumes
文件夹的已启动代理
赞成选项 3 和 4,它们以低廉的成本通知更改,不鼓励定期轮询的前两个选项。
使用 AppleScript 的 delay
命令、shell 实用程序(例如 sleep
),甚至是紧密的重复循环都应该避免,因为它们往往会阻塞用户界面当他们 运行宁。
可以使用重复计时器来定期轮询,而不是浪费时间不断检查可能发生或可能不会发生的事情,可以使用 NSWorkspace,因为它会为此类事情提供通知(在其他人中)。它的工作方式是您的应用程序注册它感兴趣的特定通知,并在(如果)事件发生时调用指定的处理程序。
请注意,以下脚本包含语句,因此可以从脚本编辑器中 运行 作为示例 - 观察者已添加到应用程序实例中,并且会一直存在,直到它们被删除或应用程序被删除退出:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "AppKit"
use scripting additions
on run -- or whatever initialization handler
# set up notifications
tell current application's NSWorkspace's sharedWorkspace's notificationCenter
its addObserver:me selector:"volumeMounted:" |name|:(current application's NSWorkspaceDidMountNotification) object:(missing value)
its addObserver:me selector:"volumeUnmounted:" |name|:(current application's NSWorkspaceDidUnmountNotification) object:(missing value)
end tell
end run
on volumeMounted:aNotification -- do something on mount
set volumeName to (NSWorkspaceVolumeLocalizedNameKey of aNotification's userInfo) as text
display notification "The volume " & quoted form of volumeName & " was mounted." with title "Volume mounted" sound name "Hero" -- or whatever
end volumeMounted:
on volumeUnmounted:aNotification -- do something on unmount
set volumeName to (NSWorkspaceVolumeLocalizedNameKey of aNotification's userInfo) as text
display notification "The volume " & quoted form of volumeName & " was unmounted." with title "Volume unmounted" sound name "Funk" -- or whatever
end volumeUnmounted:
我有一个子程序来检查是否安装了磁盘, 我想知道如何让这个子程序总是每 5 秒 运行。
提前致谢!
on checkMyDiskIsMounted()
tell application "Finder"
activate
if exists disk "myDisk" then
--do anything
else
--do anything
end if
end tell
end checkMyDiskIsMounted
四个选项:
马特
建议的带延迟的重复循环repeat -- code delay 5 end repeat
带有
idle
处理程序的(保持打开状态)小程序on run -- do intialiations end run on idle -- code return 5 end idle
像 red_menace
建议的 AppleScriptObjC 通知
观察
/Volumes
文件夹的已启动代理
赞成选项 3 和 4,它们以低廉的成本通知更改,不鼓励定期轮询的前两个选项。
使用 AppleScript 的 delay
命令、shell 实用程序(例如 sleep
),甚至是紧密的重复循环都应该避免,因为它们往往会阻塞用户界面当他们 运行宁。
可以使用重复计时器来定期轮询,而不是浪费时间不断检查可能发生或可能不会发生的事情,可以使用 NSWorkspace,因为它会为此类事情提供通知(在其他人中)。它的工作方式是您的应用程序注册它感兴趣的特定通知,并在(如果)事件发生时调用指定的处理程序。
请注意,以下脚本包含语句,因此可以从脚本编辑器中 运行 作为示例 - 观察者已添加到应用程序实例中,并且会一直存在,直到它们被删除或应用程序被删除退出:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "AppKit"
use scripting additions
on run -- or whatever initialization handler
# set up notifications
tell current application's NSWorkspace's sharedWorkspace's notificationCenter
its addObserver:me selector:"volumeMounted:" |name|:(current application's NSWorkspaceDidMountNotification) object:(missing value)
its addObserver:me selector:"volumeUnmounted:" |name|:(current application's NSWorkspaceDidUnmountNotification) object:(missing value)
end tell
end run
on volumeMounted:aNotification -- do something on mount
set volumeName to (NSWorkspaceVolumeLocalizedNameKey of aNotification's userInfo) as text
display notification "The volume " & quoted form of volumeName & " was mounted." with title "Volume mounted" sound name "Hero" -- or whatever
end volumeMounted:
on volumeUnmounted:aNotification -- do something on unmount
set volumeName to (NSWorkspaceVolumeLocalizedNameKey of aNotification's userInfo) as text
display notification "The volume " & quoted form of volumeName & " was unmounted." with title "Volume unmounted" sound name "Funk" -- or whatever
end volumeUnmounted: