[英]Applescript alter text on clipboard
我写了一个 AppleScript 来自动化我的工作设置,只需要最后的润色。 我在脚本中将某些内容复制到剪贴板; 我需要编辑我复制的文本以仅保留最后一行(最好不要打开实际的文本编辑器应用程序,因为这会使我的工作区变得混乱),然后将该行粘贴到 Google Chrome 浏览器中。
到目前为止,在我的脚本的最后一步中,我做了一些导致消息文本到终端的 output 的事情。 然后我将终端中可见的文本复制到剪贴板,如下所示:
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 的可爱回答后,我发现情况有点复杂:
我已经知道http://88.0.0.1的形式(它始终是这个)所以有没有办法搜索它,然后用下面链接的 rest 抓住它? (如果能够用“”或换行符分隔结尾,那就太好了)
编辑 2:如果http://88.0.0.1...
多次出现,我们希望 select 只是这些 URL之一- 它们可能通常相同,但选择最后一个将是最安全的。
我需要做的是抓住 URL,启动 Google Chrome 浏览器并将其粘贴到那里。
&
这总是以格式
or *URL*
的文本结尾,例如在上面的示例中它将是or http://88.0.0.1:1932/?token=5e6d97b348fsy734gd
。 这与上面的行由行分隔符加上“或”之前的一些尾随空格分隔。
&
如果
http://88.0.0.1...
多次出现,我们希望 select 只是这些 URL之一- 它们可能通常相同,但选择最后一个将是最安全的。
如果您要做的是从前面 window中使用剪贴板中的选定选项卡的内容中获取以http http://88
开头的最后一个URL ,然后在这里打开它在谷歌浏览器中,也不必使用UI 脚本。
换句话说,无需更改剪贴板上的文本或击键URL ,因为您只需告诉Google Chrome使用URL 即可。
示例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代码就是这样,并且没有任何包含的错误处理,不包含任何可能适当的额外错误处理。 用户有责任根据需要或需要添加任何错误处理。 查看AppleScript 语言指南中的try语句和错误语句。 另请参阅处理错误。 此外,在适当的情况下,在事件之间可能需要使用延迟命令,例如delay 0.5
,并适当设置延迟的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.