【问题标题】:macOS change text when you copy it to clipboard将文本复制到剪贴板时,macOS 会更改文本
【发布时间】:2021-01-11 05:45:18
【问题描述】:

我有一些文字

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 中执行此操作?自动化?苹果脚本?

【问题讨论】:

    标签: macos replace automation applescript


    【解决方案1】:

    以下示例 AppleScript 代码展示了一种操作文本的方法你的操作:

    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
    

    结果:
    "blah blah blah&lt;br&gt;this is a new line&lt;br&gt;blah blah blah"

    您可以将其合并到 Automator Service/Quick Action 中的 Run AppleScript action,如果这样的话是你想要的。

    【讨论】:

      【解决方案2】:

      另一个:

      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
      

      【讨论】:

        【解决方案3】:

        您可能希望创建一个 Automator 服务工作流程,然后您可以通过系统偏好设置将键盘快捷键绑定到该工作流程。在工作流程中,您可以插入 Run AppleScript 操作或 Run 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>}"
        

        您需要勾选名为输出替换所选文本的复选框。要选择工作流应接收的内容,请确定您设想激活此服务的两种方案中的哪一种:

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

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

        将您的服务保存为您希望它在服务菜单中显示的任何内容。通过系统偏好设置分配一个键盘快捷键(例如V)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-22
          • 1970-01-01
          相关资源
          最近更新 更多