Copy plain text from clipboard using AppleScript

Situation

  • Open a Word document.
  • Copy some formatted texts from the document to the clipboard.
  • Paste it into the CKEditor instance

CKEditor got a smelling M $ HTML type with lots of useless html elements and styles. Even removing formatting using the CKEditor function does not display clear text.

Desired Solution

Can anyone provide an AppleScript that deletes a stylized / HTML line and pastes a piece of clear text back to the clipboard .

A plus will be a quick tip on how to bind AppleScript to a function key .

+4
source share
5 answers

You do not show how you copy and paste now. It should be possible to use something like this:

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

This will allow you to get a text version of the clipboard data, and then replace the contents of the clipboard (which contains HTML) with plain text.

To bind a script to a function key, I recommend using Automator to create a service that runs your script, and then use the System Preferences panel to assign a key. In fact, I suspect that this whole task will be better than a service that receives text as input, rather than trying to explicitly extract it from the clipboard.

+9
source

An old question, but I found that existing answers do not completely convert the text to plain text. It seems they set the font Helvetica and size to 12.

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

In terminal:

 $ pbpaste | pbcopy 

Like AppleScript:

 do shell script "pbpaste | pbcopy" 

What is it.

+3
source

set the clipboard to "Standard Add-ons". You do not need to embed it in the Word application ...

 set the clipboard to (the clipboard as text) 
+2
source

This worked for me:

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

NOTE. . When you press compile, \n converted to a literal new line. It is perfectly. It still works. I tried using echo -n , but it printed -n in the output.

0
source

echo -n does not work because the AppleScript do shell script command uses sh, not bash, and sh echo is a built-in that does not accept parameters. Specify / bin / echo explicitly and it will work:

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

This will put a text copy of my_string on the clipboard.

0
source

All Articles