将文本复制到剪贴板时 macOS 更改文本

macOS change text when you copy it to clipboard

我有一些文字

blah blah blah
this is a new line
blah blah blah

我希望能够复制上面的文字,粘贴的时候,我希望它看起来像这样

blah blah blah<br>this is a new line<br>blah blah blah

你如何在 macOS 中执行此操作?自动化?苹果脚本?

以下 示例 AppleScript 代码 显示了一种操作 text 您在 OP 中的显示方式:

set theText to "blah blah blah
this is a new line
blah blah blah"

set curTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {linefeed, return}
set theText to text items of theText
set AppleScript's text item delimiters to "<br>"
set theText to theText as text
set AppleScript's text item delimiters to curTID

return theText

Result:
"blah blah blah<br>this is a new line<br>blah blah blah"

您可以将其合并到 运行 AppleScript actionAutomator Service/Quick行动,如果这是你想要的。

另一个:

set theText to "blah blah blah
this is a new line
blah blah blah"

set curTID to AppleScript's text item delimiters
set theText to every paragraph of theText
set AppleScript's text item delimiters to "<br>"
set theText to theText as text
set AppleScript's text item delimiters to curTID

return theText

您可能想要创建一个 Automator Service 工作流,然后您可以通过系统偏好设置将键盘快捷键绑定到该工作流。在工作流程中,您可以插入 运行 AppleScript 操作,或 运行 Shell Script 操作.删除任何现有示例代码,并插入以下代码之一(取决于您选择的操作):

AppleScript:

on run {input}
    set my text item delimiters to "<br>"
    return the input's paragraphs as text
end run

Shell 脚本 将输入 传递给标准输入:

input="$(</dev/stdin)"
printf "${input//$'\n'/<br>}"

您需要勾选名为 Output replaces selected text 的复选框。要选择工作流应接收的内容,请确定您设想激活此服务的两种情况中的哪一种:

  1. 如果服务应该对您突出显示的文本进行操作,然后将其替换为修改后的输出,请选择 Workflow receives: text

  2. 如果服务应该对存储在剪贴板上的文本进行操作,然后将其插入到您当前正在处理的文档中,请选择 Workflow收到:没有输入。此外,在 运行 AppleScript运行 上方插入名为 Get Contents of Clipboard 的操作 Shell脚本动作。

将您的服务保存为您希望它在服务菜单中显示的任何内容。通过系统偏好设置.

分配键盘快捷键(例如V