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

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"

Related

Unable to make a regex work in new Mac Big Sur terminal

I am trying to make a perl onliner work in mac Big Sur terminal. The online is this
perl -pi -e 's/REGULAR_EXPRESSION_TO_BE FOUND/REPLACEMENT/g' *.hmtl
When I try to search and replace in editor BBEDITOR it works fine, but when I try in macOS terminal it does not replace. I believe it may have to do with encoding since I am working with Spanish texts. But my texts are in UTF-8.
If your regex or replacement text are unicode, you need the utf8 pragma to tell Perl to decode the command line script as unicode. Otherwise they will be interpreted as bytes or otherwise according to your locale. Just because it looks like the right character to you on your terminal doesn't mean it really is. This is because the terminal does its own decoding of the bytes you pasted or typed or printed.
Add -Mutf8 to the command line or do use utf8. You can always use the B::perlstring function to see what Perl thinks of what you typed.
# v5.22
$ perl -e 'use B; print B::perlstring "愛"; '
"\346\204\233"
$ perl -Mutf8 -e 'use B; print B::perlstring "愛"; '
"\x{611b}"
$ perl -e 'print "\346\204\233"; '
愛
The regex and the file have to be in the same encoding for the matching to work. Because obviously "\346\204\233" != "\x{611b}". To remove the ambiguity of the terminal you might have to write a short script file to debug it. You also might need -CSD as well.
See here for more information.
perlrun
utf8 pragma
HTH

Read Double byte characters in plist from shell

I am working on Mac. I have a p-list entry containing double byte chinese characters,
ie.ProductRoot /Users/labuser/Desktop/您好.
Now i am running this command on terminal
defaults read "path to p-list" ProductRoot
and I am getting /Users/labuser/Desktop/\u60a8\u597d
........How can i fix this?
"defaults read" doesn't seem to have any way to change the format of the output. Maybe you could pipe that to another command-line tool to unescape the Unicode characters.
Failing that, it'd be very easy to write a tool in Objective-C or Swift to dump just that one value as a string.
As a side note, you claim the file has double-byte characters. If it's being created by native Mac code, it's probably more-likely to be in UTF-8 encoding. I don't know if that would matter at all, but I figured I'd add that in case it's relevant.
You could try this:
defaults read | grep ppt | perl -npe 's/\\\\U(\w\w\w\w)/chr hex $1/ge'

Internationalization of file names in applescript

Regarding this question and this answer, this a problem I encounter often. How can I tell applescript to correctly understand the file names I’m giving it, i.e. understand all characters, not just ASCII ones.
The read and write commands still default to the "primary encoding", like MacRoman or MacJapanese. You can use UTF-8 by adding as «class utf8»:
$ printf äあ>/tmp/a
$ osascript -e 'read "/tmp/a"'
äあ
$ osascript -e 'read "/tmp/a" as «class utf8»'
äあ
as Unicode text is UTF-16.

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)
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.

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.

Resources