Copy pure text from clipboard using AppleScript
Solution 1
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.
Solution 2
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.
Solution 3
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)
Solution 4
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.
SteAp
Fiddling around with Flutter. Writing Frog (spare-time project). Profile image: Please don't copy my painting.
Updated on June 04, 2022Comments
-
SteAp about 2 years
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.
-
SteAp over 11 yearsThx! I'll give it a try. Shouldn't the script tell FINDER instead of WORD? Just to make it app-neutral?
-
Ken Thomases over 11 yearsThe documentation of the clipboard commands in the StandardAdditions dictionary all say "Use inside a ‘tell’ block and activate the application first" or similar. I agree that, empirically, it seems to work without that.
-
Ken Thomases over 11 yearsWell, you had indicated you wanted to copy from Word. If you want to copy from the active app, then you could try adayzdone's approach or you could use System Events to get the active app and tell that.