Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy pure text from clipboard using AppleScript

Situation

  • Open a Word Document.
  • Copy some formatted text from inside the document to the clipboard.
  • Paste it into an instance of CKEditor

CKEditor received smelling M$-style HTML with tons of useless html elements and styles. Even removing formatting using CKEditor's feature does not render pure text.

Desired solution

Could anybody provide an AppleScript, which removes the styled-/HTML-string and pastes the pure text part back to clipboard.

A plus would be a short hint, how to bind the AppleScript to function key.

like image 627
SteAp Avatar asked Mar 03 '13 21:03

SteAp


2 Answers

echo -n doesn't work because AppleScript's do shell script command uses sh, not bash, and sh's echo is a builtin that doesn't accept options. Specify /bin/echo explicitly and it will work:

do shell script "/bin/echo -n " & quoted form of my_string & " | pbcopy"

That will put a plain text copy of my_string on the clipboard.

like image 144
user3728431 Avatar answered Oct 20 '22 13:10

user3728431


Old question, but I found that the existing answers do not completely convert the text to plain. They seem to set the font to Helvetica and the size to 12.

However, you can pipe pbpaste and pbcopy to really remove formatting.

In the Terminal:

$ pbpaste | pbcopy

As an AppleScript:

do shell script "pbpaste | pbcopy"

That's it.

like image 22
noamtm Avatar answered Oct 20 '22 11:10

noamtm