SteAp
SteAp

Reputation: 11999

Copy pure text from clipboard using AppleScript

Situation

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.

Upvotes: 12

Views: 19794

Answers (5)

noamtm
noamtm

Reputation: 12953

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.

Upvotes: 7

John Gruber
John Gruber

Reputation: 31

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.

Upvotes: 3

james2doyle
james2doyle

Reputation: 1429

This worked for me:

do shell script "echo " & total_paying & " | tr -d \"\n\" | pbcopy"

NOTE: When you click compile, the \n will be converted to a literal newline. This is fine. It still works. I tried using echo -n but it was printing the -n in the output.

Upvotes: 0

adayzdone
adayzdone

Reputation: 11238

set the clipboard to is defined in Standard Additions. You don't need to enclose it in a tell application "Word" ...

set the clipboard to (the clipboard as text)

Upvotes: 3

Ken Thomases
Ken Thomases

Reputation: 90521

You don't show how you're copying and pasting currently. It should be possible to use something like this, though:

tell application "Word"
    set theData to (the clipboard as text)
    set the clipboard to theData
end tell

That will obtain the plain text version of the clipboard data and then replace the clipboard contents (which contains HTML) with the plain text.

To bind the script to a function key, I recommend using Automator to make a service that runs your script and then use the Keyboard pane of System Preferences to assign a key. In fact, I suspect this whole task would be better as a service that receives the text as input rather than attempting to explicitly fetch it from the clipboard.

Upvotes: 21

Related Questions