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.


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.

  • 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. – rubik's sphere Jun 10 '17 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. – rubik's sphere Jun 10 '17 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. – user3439894 Jun 10 '17 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? – user3439894 Jun 10 '17 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. – rubik's sphere Jun 11 '17 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
  • I tested your code, but it still puts the rich text on the clipboard. Does this solution work properly in your environment? – rubik's sphere Jun 10 '17 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

  • 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. – rubik's sphere Jun 10 '17 at 7:20

You must log in to answer this question.

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