如何检查用AppleScript粘贴到剪贴板的值
问题描述:
我是AppleScript noob,我真的很想用它做一些很好的事情。我怎样才能使AppleScript在任何时候运行检查剪贴板更改?我想要做的是检查剪贴板,看看它是否是一个特定的值,并使用该剪贴板值进行网络请求。如何检查用AppleScript粘贴到剪贴板的值
这是我现在有,它只是获取在当前剪贴板
get the clipboard
set the clipboard to "my text"
任何帮助,将不胜感激的价值。提前致谢。
答
“保持打开”应用程序与
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