简体   繁体   English

Applescript 更改剪贴板上的文本

[英]Applescript alter text on clipboard

I have written an AppleScript to automate my work setup and just need the final touch.我写了一个 AppleScript 来自动化我的工作设置,只需要最后的润色。 I copy something to clipboard in my script;我在脚本中将某些内容复制到剪贴板; I need to edit the text I copied to leave only the last line (preferably without opening an actual text editor application, as this would clutter my workspace) and just paste that line in the Google Chrome browser.我需要编辑我复制的文本以仅保留最后一行(最好不要打开实际的文本编辑器应用程序,因为这会使我的工作区变得混乱),然后将该行粘贴到 Google Chrome 浏览器中。


In the last step of my script so far I do something causing message text to be output to Terminal.到目前为止,在我的脚本的最后一步中,我做了一些导致消息文本到终端的 output 的事情。 Then I get the text visible in Terminal to be copied to the clipboard as follows:然后我将终端中可见的文本复制到剪贴板,如下所示:

tell application "Terminal"
    tell front window
        set the clipboard to contents of selected tab as text
    end tell
end tell

Now I can for example paste it, and it's something like现在我可以粘贴它,就像

...
[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

This always ends with text of the format or *URL* , eg in the above example it would be or http://88.0.0.1:1932/?token=5e6d97b348fsy734gd .这总是以格式or *URL*的文本结尾,例如在上面的示例中它将是or http://88.0.0.1:1932/?token=5e6d97b348fsy734gd This is separated from the line above by a line delimiter plus some trailing white-space before "or".这与上面的行由行分隔符加上“或”之前的一些尾随空格分隔。

What I need to do is grab that URL, start a Google Chrome browser and paste it in there.我需要做的是抓住 URL,启动 Google Chrome 浏览器并将其粘贴到那里。

For opening Google Chrome, it should be simple enough, the following code (from this tut ) takes you to Instagram:打开谷歌浏览器应该很简单,下面的代码(来自这个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

and literally all I need is something which edits what's on the clipboard to just the *URL* (in the case of the above example, just http://88.0.0.1:1932/?token=5e6d97b348fsy734gd ).从字面上看,我需要的只是将剪贴板上的内容编辑为*URL* (在上面的例子中,只是http://88.0.0.1:1932/?token=5e6d97b348fsy734gd )。 After that I can paste it from the clipboard (where Instagram is typed in, in the tutorial).之后,我可以从剪贴板(在教程中输入 Instagram 的位置)粘贴它。


After trying the lovely answer by @user3439894 below I found the situation is a bit more complicated:在尝试了下面@user3439894 的可爱回答后,我发现情况有点复杂:

  1. there are an unpredictable number of blank lines "" after the final link.在最终链接之后有不可预测的空白行“”。
  2. I'm also wondering whether there may be white-space at the end as well我也想知道最后是否还有空白

I already know the form of http://88.0.0.1 (it's always consistently this) so is there a way of maybe searching for that and then just grabbing it with the rest of the link that follows?我已经知道http://88.0.0.1的形式(它始终是这个)所以有没有办法搜索它,然后用下面链接的 rest 抓住它? (it would be great to be able to delimit the end by either " " or new-line) (如果能够用“”或换行符分隔结尾,那就太好了)

Edit 2: In case http://88.0.0.1... occurs multiple times, we would like to select just one of these URLs - they are probably generally the same, but selecting the last would be safest.编辑 2:如果http://88.0.0.1...多次出现,我们希望 select 只是这些 URL之一- 它们可能通常相同,但选择最后一个将是最安全的。

What I need to do is grab that URL, start a Google Chrome browser and paste it in there.我需要做的是抓住 URL,启动 Google Chrome 浏览器并将其粘贴到那里。

& &

This always ends with text of the format or *URL* , eg in the above example it would be or http://88.0.0.1:1932/?token=5e6d97b348fsy734gd .这总是以格式or *URL*的文本结尾,例如在上面的示例中它将是or http://88.0.0.1:1932/?token=5e6d97b348fsy734gd This is separated from the line above by a line delimiter plus some trailing white-space before "or".这与上面的行由行分隔符加上“或”之前的一些尾随空格分隔。

& &

In case http://88.0.0.1... occurs multiple times, we would like to select just one of these URLs - they are probably generally the same, but selecting the last would be safest.如果http://88.0.0.1...多次出现,我们希望 select 只是这些 URL之一- 它们可能通常相同,但选择最后一个将是最安全的。

If what you are trying to do is get the last URL that starts with http://88 from the contents of the selected tab of the front window in Terminal , then here is a way to do it without using the clipboard, and open it in Google Chrome , and also do it without having to use UI Scripting .如果您要做的是从前面 window中使用剪贴板中的选定选项卡内容中获取以http http://88开头的最后一个URL ,然后在这里打开它在谷歌浏览器中,也不必使用UI 脚本

In other words, no need to alter the text on the clipboard or keystroke the URL as you can just tell Google Chrome what to use for the URL .换句话说,无需更改剪贴板上的文本或击键URL ,因为您只需告诉Google Chrome使用URL 即可

Example AppleScript code :示例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

Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate.注意:示例AppleScript代码就是这样,并且没有任何包含的错误处理,不包含任何可能适当的额外错误处理 The onus is upon the user to add any error handling as may be appropriate, needed or wanted.用户有责任根据需要或需要添加任何错误处理 Have a look at the try statement and error statement in the AppleScript Language Guide .查看AppleScript 语言指南中的try语句错误语句 See also, Working with Errors .另请参阅处理错误 Additionally, the use of the delay command may be necessary between events where appropriate, eg delay 0.5 , with the value of the delay set appropriately.此外,在适当的情况下,在事件之间可能需要使用延迟命令,例如delay 0.5 ,并适当设置延迟

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM