简体   繁体   中英

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 .

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.

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.

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)

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM