AppleScript没有办法“等待剪贴板更改”,因此您必须定期“轮询”剪贴板。
repeat
环与暂停
set oldvalue to missing value
repeat
set newValue to the clipboard
if oldvalue is not equal to newValue then
try
if newValue starts with "http://" then
tell application "Safari" to make new document with properties {URL:newValue}
end if
end try
set oldvalue to newValue
end if
delay 5
end repeat
一些可能使用的do shell script "sleep 5"
代替delay 5
;我从来没有遇到过delay
的问题,但是我从来没有在像这样的长时间运行的程序中使用它。
根据用于运行此程序的启动程序,这样的脚本可能会“束缚”应用程序并阻止它启动其他程序(某些启动程序一次只能运行一个AppleScript程序)。
“保持打开”应用程序与idle
处理器
一个更好的选择是你的程序保存为“保持打开”应用程序(在另存为...对话框),并使用idle
handler的周期性工作。
property oldvalue : missing value
on idle
local newValue
set newValue to the clipboard
if oldvalue is not equal to newValue then
try
if newValue starts with "http://" then
tell application "Safari" to make new document with properties {URL:newValue}
end if
end try
set oldvalue to newValue
end if
return 5 -- run the idle handler again in 5 seconds
end idle