gpt4 book ai didi

macos - Applescript 更改剪贴板上的文本

转载 作者:行者123 更新时间:2023-12-04 07:54:12 25 4
gpt4 key购买 nike

我已经编写了一个 AppleScript 来自动化我的工作设置,只需要最后的润色。我在脚本中将某些内容复制到剪贴板;我需要编辑我复制的文本,只留下最后一行(最好不要打开实际的文本编辑器应用程序,因为这会使我的工作区变得困惑),然后将该行粘贴到 Google Chrome 浏览器中。

到目前为止,在我的脚本的最后一步中,我做了一些导致消息文本输出到终端的事情。然后我将终端中可见的文本复制到剪贴板,如下所示:

tell application "Terminal"
tell front window
set the clipboard to contents of selected tab as text
end tell
end tell
现在我可以例如粘贴它,它就像
...
[I 15:03:31.259 LabApp] Serving notebooks from local directory: /workspace
[I 15:03:31.259 LabApp] The Jupyter Notebook is running at:
[I 15:03:31.259 LabApp] http://1fw5c518af9:1932/?token=5e6d97b348fsy734gd
[I 15:03:31.259 LabApp] or http://88.0.0.1:1932/?token=5e6d97b348fsy734gd
[I 23:56:47.798 LabApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 23:56:47.803 LabApp]

To access the notebook, open this file in a browser:
file:///root/.local/share/jupyter/runtime-open.html
Or copy and paste one of these URLs:
http://1fw5c518af9:1932/?token=5e6d97b348fsy734gd
or http://88.0.0.1:1932/?token=5e6d97b348fsy734gd
这总是以格式 or *URL* 的文本结尾,例如在上面的例子中,它是 or http://88.0.0.1:1932/?token=5e6d97b348fsy734gd .这与上面的行通过行分隔符和“或”之前的一些尾随空格分开。
我需要做的是获取该 URL,启动 Google Chrome 浏览器并将其粘贴到那里。
打开谷歌浏览器,应该很简单,下面的代码(来自这个 tut )带你到Instagram:
tell application "Google Chrome" to activate

tell application "System Events"
key code 37 using command down
delay 0.5
keystroke "https://www.instagram.com/instagram"
delay 1
key code 36
delay 1

end tell
从字面上看,我所需要的只是将剪贴板上的内容编辑为 *URL* (在上面的例子中,只是 http://88.0.0.1:1932/?token=5e6d97b348fsy734gd )。之后,我可以从剪贴板粘贴它(在教程中输入 Instagram 的地方)。

在尝试了下面@user3439894 的可爱答案后,我发现情况有点复杂:
  • final 后有不可预知的空行“”
    关联。
  • 我也想知道最后是否可能有空格
    还有

  • 我已经知道 http://88.0.0.1 的形式(它总是始终如一)那么有没有一种方法可以搜索它,然后用后面的链接的其余部分捕获它? (能够以“”或换行符分隔结尾会很棒)
    编辑2:
    万一 http://88.0.0.1...发生多次,我们只想选择 这些 URL 中的一个 - 它们可能大致相同,但选择 最后 会最安全。

    最佳答案

    What I need to do is grab that URL, start a Google Chrome browser and paste it in there.


    &

    This always ends with text of the format or *URL*, e.g. in the aboveexample it would be or http://88.0.0.1:1932/?token=5e6d97b348fsy734gd. This is separatedfrom the line above by a line delimiter plus some trailing white-spacebefore "or".


    &

    In case http://88.0.0.1... occurs multiple times, we would like toselect just one of these URLs - they are probably generally thesame, but selecting the last would be safest.


    如果您要做的是获取最后一个 网址 http://88 开头来自 中前窗口的选定选项卡的内容终端 ,那么这里有一种不使用剪贴板的方法,在 中打开谷歌浏览器 ,而且无需使用 即可完成UI 脚本 .
    换句话说,无需更改剪贴板上的文本或击键 网址 正如你所知道的 谷歌浏览器 用于 的用途网址 .
    示例 AppleScript 代码:
    tell application "Terminal" to ¬
    set theTextFromTerminal to ¬
    the contents of ¬
    the selected tab of ¬
    the front window ¬
    as text

    set theURL to ¬
    the last paragraph of ¬
    (do shell script ¬
    "grep -o 'http://88.*$' <<< " & ¬
    theTextFromTerminal's quoted form & ¬
    "; exit 0")

    if theURL is "" then return
    if theURL does not start with ¬
    "http://88." then return

    tell application "Google Chrome"
    activate
    delay 1
    if exists front window then
    make new tab at ¬
    end of front window ¬
    with properties {URL:theURL}
    else
    set URL of ¬
    the active tab of ¬
    (make new window) to theURL
    end if
    end tell

    注意:示例 AppleScript 代码就是这样,没有任何包含的错误处理不包含任何可能适当的额外错误处理。用户有责任在适当的、需要的或想要的情况下添加任何错误处理。看看 try声明和 error AppleScript Language Guide 中的声明.另见, Working with Errors .此外,使用 delay在适当的情况下,事件之间可能需要命令,例如 delay 0.5 ,适当设置延迟值。

    关于macos - Applescript 更改剪贴板上的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66791303/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com