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