AppleScript does not have a way to “wait for the clipboard to change”, so you will have to “poll” the clipboard at regular intervals.
repeat
loop with a pause
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
Some might use do shell script "sleep 5"
instead of delay 5
; I have never had a problem with delay
, but I have never use it in a long-running program like this one.
Depending on the launcher used to run this program such a script might “tie up” the application and prevent it from launching other programs (some launchers can only run one AppleScript program at a time).
“Stay Open” application with idle
handler
A better alternative is to save your program as a “Stay Open” application (in the Save As… dialog) and use an idle
handler for the periodic work.
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