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

发布于 2024-09-24 09:41:50 字数 239 浏览 3 评论 0原文

我是一名 AppleScript 菜鸟,我真的很想用它做点好事。如何制作一个始终运行检查剪贴板更改的 AppleScript?我想要做的是检查剪贴板,看看它是否是某个值,然后使用该剪贴板值发出网络请求。

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

get the clipboard
set the clipboard to "my text"

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看透却不说透 2024-10-01 09:41:50

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

重复循环并暂停

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;我从来没有遇到过延迟问题,但我从未在像这样的长时间运行的程序中使用过它。

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

使用 idle 处理程序的“保持打开”应用程序

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

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 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文