MacOS – AppleScript bug: When placing plain text on the clipboard, the text is always converted to rich text

applescriptbugcopy/pastemacostext;

Run the following code to put some text on your clipboard:

set testString to "This string should be plain text."
set the clipboard to testString

If you open up a new .rtf file in TextEdit.app, and paste the clipboard into this document, you will observe that the clipboard contains rich text. Specifically, the clipboard text is of the font Helvetica and size 12.

I also tried specifying set the clipboard to (testString as string) or set the clipboard to (testString as text). But, the code always puts rich text on the clipboard. This rich text is always of the same style (as described in the previous paragraph).

Is it possible to put text on the clipboard, as plain text, in AppleScript?


OS X El Capitan, version 10.11.6.


Best Answer

Maybe copying the text to the Pasteboard using Cocoa-AppleScript will only set plain text to paste to the RTF document.

Otherwise, use the following as a workaround:

set testString to "This string should be plain text." as string
do shell script "pbcopy <<<" & quoted form of testString

The previous mentioned workaround method adds a linefeed after testString to the Clipboard and if that's unwanted behavior, use:

set testString to "This string should be plain text." as string
do shell script "printf " & the quoted form of testString & " | pbcopy"

The other option is to use Paste and Match Style ⌥⇧⌘V instead of Paste ⌘V to match what's at the insertion point.