Macos 如何使用AppleScript检查粘贴到剪贴板的值

Macos 如何使用AppleScript检查粘贴到剪贴板的值,macos,applescript,Macos,Applescript,我是一个AppleScript noob,我真的想用它做点好事。如何使AppleScript始终运行以检查剪贴板更改?我想做的是检查剪贴板,看看它是否是某个值,然后用该剪贴板值发出web请求 这是我目前拥有的,它只获取当前剪贴板中的值 get the clipboard set the clipboard to "my text" 任何帮助都将不胜感激。提前感谢。AppleScript没有“等待剪贴板更改”的方法,因此您必须定期“轮询”剪贴板 重复暂停循环 有些人可能使用do shell脚本“

我是一个AppleScript noob,我真的想用它做点好事。如何使AppleScript始终运行以检查剪贴板更改?我想做的是检查剪贴板,看看它是否是某个值,然后用该剪贴板值发出web请求

这是我目前拥有的,它只获取当前剪贴板中的值

get the clipboard
set the clipboard to "my text"

任何帮助都将不胜感激。提前感谢。

AppleScript没有“等待剪贴板更改”的方法,因此您必须定期“轮询”剪贴板

重复
暂停循环 有些人可能使用
do shell脚本“sleep 5”
而不是
delay 5
;我从来没有遇到过延迟的问题,但是我从来没有在像这样的长时间运行的程序中使用过它

根据用于运行此程序的启动器,此脚本可能会“捆绑”应用程序并阻止其启动其他程序(某些启动器一次只能运行一个AppleScript程序)

使用
空闲处理程序“保持打开”应用程序
更好的替代方法是将程序保存为“保持打开”应用程序(在“另存为…”对话框中),并使用用于定期工作的

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

AppleScript无法“等待剪贴板更改”,因此您必须定期“轮询”剪贴板

重复
暂停循环 有些人可能使用
do shell脚本“sleep 5”
而不是
delay 5
;我从来没有遇到过延迟的问题,但是我从来没有在像这样的长时间运行的程序中使用过它

根据用于运行此程序的启动器,此脚本可能会“捆绑”应用程序并阻止其启动其他程序(某些启动器一次只能运行一个AppleScript程序)

使用
空闲处理程序“保持打开”应用程序
更好的替代方法是将程序保存为“保持打开”应用程序(在“另存为…”对话框中),并使用用于定期工作的

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