I'm an AppleScript noob, and I really want to do something nice with it. How can I make an AppleScript that runs at all times checking for clipboard changes? What I want to do is check the clipboard, see if it's a certain value, and make a web request with that clipboard value.
This is what I currently have, it just gets the value that's in the current clipboard
get the clipboard
set the clipboard to "my text"
Any help will be GREATLY appreciated. Thanks in advance.
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).
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.