Copy pure text from clipboard using AppleScript - macos

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)

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.

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.

Related

Does this AppleScript/shell code expose input in plain text in any way?

I would appreciate response from anyone with knowledge of nuts'n'bolts of Unix/macOS.
Does running this code on a local machine expose the input in plain text to any processes/logs/etc.?
I'm obviously most interested in the bracket of background processes occurring from when the keystrokes are captured to when the hashing occurs.
do shell script "md5 -q -s " & quoted form of text returned of (display dialog "Input I would like to keep unexposed:" default answer "" default button 1 buttons {"Make hash"} with hidden answer)
Yes it does expose it. At the very least, the command and its arguments (including the input string) are available via the ps command (and probably other interfaces) for as long as the command is running.

Bash shells script 101 - variable definition, pbcopy, etc

I am trying to create a (my first) bash script, but I need a little help. I have the following:
#!/bin/bash
echo "Write a LaTeX equation:"
read -e TeXFormula
URIEncoded = node -p "encodeURIComponent('$(sed "s/'/\\\'/g" <<<"$TeXFormula")')"
curl http://latex.codecogs.com/gif.latex?$URIEncoded -o /Users/casparjespersen/Desktop/notetex.gif | pbcopy
I want it to:
Require user input (LaTeX equation)
URIEncode user input (the top result in Google was using node.js, but anything will do..)
Perform a cURL call to this website converting equation to a GIF image
Copy the image to the placeholder, so I can paste it to a note taking app like OneNote, Word, etc.
My script is malfunctioning in the following:
URIEncoded is undefined, so there is something wrong with my variable definition.
When I copy using pbcopy the encrypted text content of the image is copied, and not the actual image. Is there a workaround for this? Otherwise, the script could automatically open the image and I could manually Cmd + C the content.
URIEncoded is undefined, so there is something wrong with my variable
definition.
The line should read
URIEncoded=$(node -p "encodeURIComponent('$(sed "s/'/\\\'/g" <<<"$TeXFormula")')")
without spaces around the = sign, and using the $() construct to actually perform the command, otherwise, the text of the command would be assigned to the variable.
When I copy using pbcopy the encrypted text content of the
image is copied, and not the actual image. Is there a workaround for
this? Otherwise, the script could automatically open the image and I
could manually Cmd + C the content.
pbcopy takes input from stdin but you are telling curl to write the output to a file rather than stdout. Try simply
curl http://latex.codecogs.com/gif.latex?$URIEncoded | pbcopy
or, for the second option you describe
curl http://latex.codecogs.com/gif.latex?$URIEncoded -o /Users/casparjespersen/Desktop/notetex.gif && open $_

Invoke copy & paste commands from terminal

Is is possible to invoke a copy command (as if the user pressed Cmd+C) from a bash script? Basically I want to write a simple script that I run with a global hotkey and it should take the current selection from the active app, replace something and paste the result. Is this possible?
The best I could come up so far is using pbpaste and pbcopy, but I'd like to automate that if possible.
If you're just trying to modify a text selection, you could use AppleScript.
osascript -e 'try
set old to the clipboard
end try
try
delay 0.3
tell application "System Events" to keystroke "c" using command down
delay 0.2
set text item delimiters to linefeed
set input to (paragraphs of (the clipboard as text)) as text
set the clipboard to do shell script "shopt -u xpg_echo; echo -n " & quoted form of input & " | rev" without altering line endings
tell application "System Events" to keystroke "v" using command down
delay 0.05
end try
try
set the clipboard to old
end try'
The first delay is for releasing modifier keys if the script is run with a shortcut that has other modifier keys than command. The second delay could also be reduced to something like 0.05, but long selections or for example web views often need a longer delay. Without the third delay, the clipboard would sometimes be set to old before the text would get pasted.
the clipboard as text and do shell script convert line endings to carriage returns by default. shopt -u xpg_echo is needed because the echo in sh interprets backslashes inside single quotes by default. If the input is longer than getconf ARG_MAX bytes, you can't use echo and have to either write it to a temporary file or use pbpaste.
pbpaste and pbcopy replace non-ASCII characters with question marks by default in the environment used by do shell script You can prevent that by setting LC_CTYPE to UTF-8.
Telling System Events to click menu bar items would often be even slower, and it wouldn't work in applications that don't have a menu bar or in full screen windows.
Another option would be to create an Automator service. But they also have small delays before they are run. There's a bug where the shortcuts for services don't always work until the services menu has been shown once on the menu bar. And the services aren't available when the frontmost application doesn't have a menu bar or a services menu.

Getting RTF data out of Mac OS X pasteboard (clipboard)

According to the man page for pbpaste,
-Prefer {txt | rtf | ps}
tells pbpaste what type of data to look for in the pasteboard
first. As stated above, pbpaste normally looks first for plain
text data; however, by specifying -Prefer ps you can tell
pbpaste to look first for Encapsulated PostScript. If you spec-
ify -Prefer rtf, pbpaste looks first for Rich Text format. In
any case, pbpaste looks for the other formats if the preferred
one is not found. The txt option replaces the deprecated ascii
option, which continues to function as before. Both indicate a
preference for plain text.
However (in my experience with 10.6 Snow Leopard at least), pbpaste -Prefer rtf never, ever gives up the RTF data even when it exists on the pasteboard. Is there any other simple way to get the RTF text of whatever’s ready to be pasted?
I tried AppleScript, but osascript -e 'the clipboard as «class RTF »' gives the response «data RTF 7Bton of Hex encoded crap7D». Can AppleScript convert this hexdata into text I can play with?
I can't see any way to do it from inside AppleScript, but since you're working in the shell anyway, I'd just post-process it: the "hex-encoded crap" is the RTF data you want. The simplest script I can think of is
perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))'
An explanation: substr($_,11,-3) strips off the «data RTF and »\n bits (each of the guillemets is two bytes); pack("H*", ...) packs hex-encoded data into a bytestream; unpack("C*", ...) unpacks a bytestream into an array of character values; print chr foreach ... converts each integer in the array to its corresponding character and prints it; and the -ne options evaluate the script given for each line, with that line implicitly stored in $_. (If you want that script in its own file, just make sure the shebang line is #!/usr/bin/perl -ne.) Then, running
osascript -e 'the clipboard as «class RTF »' | \
perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))'
will give you raw RTF output.
I think that at least on OS X 10.8 this would work if you copied HTML content from Chrome:
osascript -e 'the clipboard as "HTML"'|perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))'
In my experience it's impossible to get RTF data out of pbpaste, even if the man page says otherwise.
The simplest solution is to use pbv instead, which was developed exactly to work around the limitations of pbpaste.
An example: after copying the following rich text string into your clipboard:
"Hi, I'm rich text"
pbv is able to give you back proper RTF data:
$ pbv public.rtf | textutil -stdin -info
File: stdin
Type: rich text format (RTF)
Length: 19 characters
Contents: "Hi, I'm rich text"
Whereas pbpaste will always output plain text even when instructed to prefer RTF:
$ pbpaste -Prefer rtf | textutil -stdin -info
File: stdin
Type: plain text
Length: 19 characters
Contents: "Hi, I'm rich text"
Found via this similar question.
i found a conversation about this with a quick google search
It is very easy via AppleScript (tested in 10.11 El Capitan):
set the clipboard to (the clipboard as «class RTF »)
You can create a Service via Automator:
open Automator
make new service ("Dienst" in German)
add "execute a AppleScript"
input: nothing; output; replaces Selection
The Script:
-- name: convert to RTF
on run {input, parameters}
set the clipboard to (the clipboard as «class RTF »)
return the clipboard
end run
Done. Now save the new Service and to try it out: Select a text, then go to the Application Menu and choose "Services" > "convert to RTF"

Can AppleScript take output of shell script and put in paste buffer?

Can AppleScript take the output of a shell script or variable and put it in the paste buffer?
I have a file with a password for every day (formatted "date,password") and I want to write a script that when run will look up the date and output the password for that date.
That part is not a problem, I'm just wondering if there is a way to get the output to automatically go into the paste buffer?
Using AppleScript to put output of a shell command into the clipboard:
set the clipboard to (do shell script "ls")
If you do not want to use AppleScript you can use pbcopy in the shell:
sh$ some command | pbcopy

Resources