2

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.


1
  • Belatedly, this isn't completely accurate. Whenever (almost always) you copy something to the clipboard, multiple things and formats are put there — even when it is 'plain' text. In general, when you copy some text, there is also a rich text version that (I assume) is used whenever you paste into a rich text document. Note that TextEdit seems to have its own weird rules about stuff so I don't see how a general statement can be made that's based on its behaviour. I just pasted some text from this page (some comment text) and it was formatted as Times. And TextEdit offers 'match style' as well.
    – Mockman
    Nov 13, 2022 at 10:32

3 Answers 3

2

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.

5
  • I can confirm that your code does implement the desired outcome of pasting the string as plain text. However, both code samples reveal a new AppleScript bug: if testString contains an em dash (—) or an en dash (–), then these dash characters are converted to mojibake. Regarding your ultimate suggestion, the issue with "Paste and Match Style" is that this command is not scripted into all applications. Jun 10, 2017 at 6:55
  • Interestingly, this Stack Overflow answer that I found (which cites the exact bug that I described in my original post) approaches the problem from the same angle. Jun 10, 2017 at 7:03
  • @rubik's sphere, you said, "Regarding your ultimate suggestion, the issue with "Paste and Match Style" is that this command is not scripted into all applications.", I mentioned ⌥⇧⌘V because you said TextEdit in the OP so I assumed you're using TextEdit. That said, many applications do have ⌥⇧⌘V although that may call it Paste Unformatted Text or similar and many apps have Paste Special... ⇧⌘V which can handle this as well. Yes, not all apps may have any or all of these however, you're going to have to work with what you've got if using Cocoa-AppleScript cant't resolve the issue. Jun 10, 2017 at 14:56
  • @rubik's sphere, BTW, EM DASH and EN DASH are not ASCII (plain) Text and in part probably why you get mojibake. That said, AppleScript, as you've come to find out, has lots of bugs, there's no doubt about it especially since similar type operations in other apps work as expected unlike how AppleScript can and does mangle some things. If you can't find a fix/workaround to use with AppleScript, then you'll have to use a different language/method to accomplish the goal. Another option is to just manually correct the occasional occurrences of mojibake. Is that such big deal? Jun 10, 2017 at 14:56
  • 1. On the "⌥⇧⌘V" point, I only mentioned TextEdit in the OP because that is how I concluded that the clipboard set by AppleScript contains rich text. This was my question: "Is it possible to put text on the clipboard, as plain text, in AppleScript?" The script in question does not paste or keystroke the text. The script merely places the text on the clipboard (for the user to paste this text wherever they want). 2. No, the em dash/en dash bug is not a big deal. Your solution fulfills my needs. I will implement your code, and I will add a replace_chars statement to account for this bug. Jun 11, 2017 at 3:13
0

This should work:

set the clipboard to string of (the clipboard as record)

In fact, you can safety-proof it using a try statement:

try
  set the clipboard to string of (the clipboard as record)
on error errMsg
  display dialog errMsg
end try
1
  • I tested your code, but it still puts the rich text on the clipboard. Does this solution work properly in your environment? Jun 10, 2017 at 4:30
0

It looks like no matter what you do, if you have plain text selected in your new document tab in your text edit preferences, your clipboard will paste plain text. However if you have rich text enabled, it looks as if everything will be pasted as rich text in a rtf document.

enter image description here

enter image description here

I also tried selecting the text in the RTF document and tried to convert to plaintext In the format menu. A dialog window popped up saying that the document would be converted from RTF to plain text If I chose to format the text

1
  • Try the following to witness the issue that I am talking about: Create an .rtf document in TextEdit. Then, type some text with any font other than Helvetica, and in any size other than 12. Now, paste the clipboard (whose text was set by the AppleScript code). You will observe that the font of this text has been changed to Helvetica, and the size has been changed to 12. This means that the text on the clipboard is rich text; the unique font data and the unique size data are contained on the clipboard. Jun 10, 2017 at 7:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .