【问题标题】:Apple Script - How to I append the clipboard with formatting preserved to a note in Apple Notes?Apple Script - 如何将保留格式的剪贴板附加到 Apple Notes 中的注释?
【发布时间】:2020-04-08 00:47:39
【问题描述】:

我创建了一个脚本,可以将剪贴板上的任何内容附加到 Apple 笔记中。但是,不保留附加文本的格式,甚至不保留行格式。 如何在保留剪贴板的 LINE 格式的同时将剪贴板附加到便笺?我不太关心其他格式,尽管最好尽可能保留它。

另外,我希望将文本作为新行附加,在预先存在的和附加的文本之间有一个换行符,并且整个笔记的文本大小(包括预先存在的和附加的文本)到为 18 分。

set AppendText to (the clipboard)


tell application "Notes"
    tell account "iCloud"
        tell folder "Clipboard"
            set OriginalText to the body of note 1 -- the contents of the notes are encoded in HTML
        end tell
    end tell
end tell



tell application "Notes"
    tell account "iCloud"
        tell folder "Clipboard"
            set body of note 1 to {"<div style=\"font-size: 18px\">" & OriginalText & "<br>" & AppendText & "</div>"}
        end tell
    end tell
end tell

假设注释的预先存在的文本是

Original text line 1

Original text line 2

Original text line 3

而需要追加的文字是

Append text line 1

Append text line 2

Append text line 3

当我运行脚本时,注释的文本设置为

Original text line 1

Original text line 2

Original text line 3

Append text line 1 Append text line 2 Append text line 3

我希望它是这样的

Original text line 1

Original text line 2

Original text line 3

Append text line 1

Append text line 2

Append text line 3

【问题讨论】:

  • 复制粘贴?
  • 我不完全明白你的意思。你可以说得更详细点吗? @马特
  • 您的示例适用于我使用纯文本 - 剪贴板上的格式是什么?
  • 剪贴板的格式将是纯文本,但据我所知,即使是纯文本也包含行格式。例如,如果我使用行格式(例如,换行符、新行)将任何文本复制到剪贴板,然后运行脚本display dialog (the clipboard) as text,对话框将显示保留行格式的文本。但是,我在上述问题中的主要脚本不保留行格式。 当我将剪贴板附加到笔记时,如何保留其行格式? @red_menace
  • 我更新了我的问题;希望现在更清楚了。不过,感谢到目前为止的所有帮助。 @red_menace

标签: macos terminal applescript macos-catalina


【解决方案1】:

由于便笺的正文是 HTML,一种解决方案是使用textutil 实用程序在添加之前转换附加文本(便笺应用程序处理合并 HTML),例如:

set appendText to (the clipboard)

set convertedText to (do shell script "echo " & quoted form of appendText & " | textutil -convert html -excludedelements '(p)' -stdin -stdout")

tell application "Notes"
    tell folder "Whatever" -- example
        set originalText to body of note 1
        set body of note 1 to originalText & convertedText
    end tell
end tell

【讨论】:

  • 我刚试过,但出于某种奇怪的原因,附加文本中的换行符被重复了,所以应该有两个换行符。知道为什么吗? @red_menace
  • 每一行都有一个换行符 - 中间是否有额外的空白行?
  • 不。自己试试。更具体地说,尝试多次运行快捷方式而不更改剪贴板中的内容。此外,请确保原始注释的文本和您复制的文本有换行符。如果您遇到同样的问题,请告诉我。 @red_menace
  • 我在发布之前对其进行了测试,使用纯文本和富文本。您还可以将附加文本设置为字符串进行测试,例如"this is a test" &amp; return &amp; "this is a test" &amp; return &amp; "this is a test"
  • 其他尝试是排除段落标签,因为这会增加一些空白行间距。我已更新我的帖子以包含该选项。
【解决方案2】:

感谢 red_menace 和 user3439894,我已经完成了脚本。在这里。

set appendText to (the clipboard)

set convertedText to (do shell script "echo " & quoted form of appendText & ¬
    " | textutil -convert html -fontsize 18 -excludedelements '(p)' -stdin -stdout")

tell application "Notes"
    tell account "iCloud"
        tell folder "Clipboard"
            set originalText to the body of note 1
            if originalText as string is "" then
                set body of note 1 to {"<div style=\"font-size: 18px\">" & convertedText & "</div>"}
            else
                set body of note 1 to {"<div style=\"font-size: 18px\">" & originalText & ¬
                    "</div><div><span style=\"font-size: 18px\"><br></span></div> <div style=\"font-size: 18px\">" & ¬
                    convertedText & "</div>"}
            end if
        end tell
    end tell
end tell

【讨论】:

  • 谢谢!这要简单得多。我会用那个。
猜你喜欢
  • 1970-01-01
  • 2021-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-21
  • 1970-01-01
  • 2020-01-01
相关资源
最近更新 更多