Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

An AppleScript to convert clipboard contents to plain text System
I frequently find myself wanting to copy and paste text between two programs that use completely different styles for their text. The problem is that when I paste the text, it brings along the style of other program, which means I now have to change font, weight, size, etc. for the text to make it look like the text I'm pasting it beside. However, If you have quick access to AppleScripts through Apple's Script Menu or iKey, then the following line of AppleScript will remove the formatting and leave the plain text (which should then take on the default formatting of whatever program you are pasting it into):
set the clipboard to «class ktxt» of ((the clipboard as text) as record)
How it works: The as text portion grabs the stylized text from whatever other junk may be surrounding it (which can be significant). As record splits out the style and text portions, and »class ktxt» grabs the plain text portion of that.

[robg adds: Enter the above line in Script Editor and save it (I saved it as a compiled application, though I don't think this matters). Save it into your user's Library -> Scripts folder so that it's accessible everywhere. Then, when you have text to convert, just choose the script from the drop-down Script menu prior to pasting, and you'll get a nice plain-text paste.]
    •    
  • Currently 2.80 / 5
  You rated: 5 / 5 (5 votes cast)
 
[30,395 views]  

An AppleScript to convert clipboard contents to plain text | 21 comments | Create New Account
Click here to return to the 'An AppleScript to convert clipboard contents to plain text' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to convert clipboard contents to plain text
Authored by: NeutronMonk on Feb 11, '04 11:26:17AM

Works as advertised. I pasted a selection from TextEdit into Word and the selection adopted the style currently in use in Word. Very handy indeed!



[ # ]
An AppleScript to convert clipboard contents to plain text
Authored by: Nasir on Feb 11, '04 11:40:12AM
Just for the record: There is a small, faceless Cocoa application called Plain Clip that does the very same thing.

[ # ]
Plain Clip
Authored by: spaminacan on Feb 11, '04 12:12:25PM
Sweeeeet! Thank you for pointing this out. Gave it a hot key via Another Launcher (http://www.versiontracker.com/dyn/moreinfo/macosx/20035) and I'm set. As a student, Plain Clip will cumulatively save me hours per year in reformatting time. Hints like this are why I love this site!

[ # ]
An AppleScript to convert clipboard contents to plain text
Authored by: Tom Robinson on Feb 11, '04 01:34:19PM

Some applications can strip formatting when pasting, which saves a step:

FileMaker has command-option-V

TextEdit has command-shift-option-V

Word (from memory) has a Paste As Plain Text option.

Cheers



[ # ]
An AppleScript to convert clipboard contents to plain text
Authored by: hamarkus on Feb 11, '04 02:12:57PM

It is Command-Option-I in Word.

This is something I use constantly, and really miss in a lot of apps, e.g. TextEdit (Shift-Option-Command-V does not work for me under 10.2.8, maybe a 10.3-only thing).
Using Plain Clip or the script menu, it is still a three step thing: 'press shortcut', 'switch back to current app', 'paste' or 'click on script menu', 'click on the actual script', 'paste'.



[ # ]
iKey or similar might help
Authored by: jecwobble on Feb 12, '04 10:35:11AM

I can't try this right now, but I would think one could write an AppleScript that performs this tip's line first, then performs a paste action (is that possible in AppleScript?). Then come up with a unique global key sequence in iKey (or other tool) to call that script.

Then it's just the usual two step process: Cmd+C to copy and custom-key-sequence to paste.



[ # ]
Enabling the script menu
Authored by: hamarkus on Feb 11, '04 02:24:17PM

How again do I enable the script menu in the menu bar?



[ # ]
Enabling the script menu
Authored by: cfoster on Feb 11, '04 04:17:19PM

There's a link in the original post to the location on Apple's site where you can find the ScriptMenu installer.

Here it is:
http://www.apple.com/applescript/scriptmenu/index.html



[ # ]
Enabling the script menu
Authored by: rgray on Feb 11, '04 08:43:45PM

Applications->Applescript->Install Script Menu in a vanilla 10.3.2



[ # ]
Simpier version
Authored by: paulrob on Feb 11, '04 03:31:40PM
If you use type "text" your paste size will be limited as text can't handle an unlimited length of characters. But type "string" has no length restriction, and as a bonus strips the formatting. Like so:-

set the clipboard to (the clipboard as string)


[ # ]
Simpier version
Authored by: cfoster on Feb 11, '04 04:13:19PM

This does not work. "As String" keeps style information.

In your testing, you must have been copying from something that didn't include style, or pasting to something that doesn't accept it.



[ # ]
Simpier version
Authored by: paulrob on Feb 12, '04 02:06:26PM

Hmm! Curses! You're 99% right.

Actually "as string" does strip all fonts and styles. See p.85 of Applescript Language Guide.pdf. Unfortunately, it then applies a Geneva font to the text! But WHY???

As I was pasting to documents written with a Geneva font I thought I'd been successful.



[ # ]
An AppleScript to convert clipboard contents to plain text
Authored by: nicksay on Feb 11, '04 04:23:55PM
A much simpler script is as follows:

#!/bin/sh
pbpaste | pbcopy
Put it in a plain text file, save it and put it in your Scripts folder. Voila.

[ # ]
An AppleScript to convert clipboard contents to plain text
Authored by: cfoster on Feb 11, '04 04:46:00PM

Yep, that works! Nice!



[ # ]
chmod 777 to make the script executable
Authored by: SimonDorfman.com on Feb 12, '04 12:24:53AM
Open terminal, type:
chmod 777 /Library/Scripts/Clipboard-\>Plain\ Text\ \(shell\ script\).txt
replace
Clipboard-\>Plain\ Text\ \(shell\ script\).txt
with whatever you named the script.

[ # ]
chmod 777 to make the script executable
Authored by: pauljlucas on Feb 12, '04 11:32:34AM

That really should be 755.

---
- Paul



[ # ]
An AppleScript to convert clipboard contents to plain text
Authored by: SimonDorfman.com on Feb 11, '04 10:09:19PM
I'd like to alter this script to make everything plain text except bold and italics which would remain bold and italics. Everything else would be dropped (color, font, indents, etc.).

Any idea how I'd do this?

[ # ]
i got this working, here's the script:
Authored by: SimonDorfman.com on Feb 14, '04 06:01:03PM
this requires this script to run.

property sttr : missing value

--> modify the following as needed
if sttr is missing value then
	set sttr to load script alias "Macintosh HD:Library:Scripts:StyledTextToRecord:StyledText <-> Record.scpt"
end if


display dialog "I will take the contents of the clipboard (styled text, please) and convert everything to plain text, except for bolds and italics" with icon note

set clipContents to (the clipboard) as styled text

tell sttr to copy StyledTextToRecord(clipContents) to styleInfo

--> get default styles to reset all
tell sttr to copy StyledTextToRecord("x" as Unicode text) to defaultStyleInfo

repeat with i from 1 to count styleInfo's stylesOn
	set x to styleInfo's stylesOn's item i
	if x is not {plain} then
		if x contains bold and x contains italic then
			set styleInfo's stylesOn's item i to {bold, italic}
		else if x contains bold then
			set styleInfo's stylesOn's item i to {bold}
		else if x contains italic then
			set styleInfo's stylesOn's item i to {italic}
		else
			set styleInfo's stylesOn's item i to {plain}
		end if
	end if
	--> colors and others out!
	set styleInfo's stylesHeights's item i to defaultStyleInfo's stylesHeights's item 1
	set styleInfo's stylesAscents's item i to defaultStyleInfo's stylesAscents's item 1
	set styleInfo's stylesFontIDs's item i to defaultStyleInfo's stylesFontIDs's item 1
	set styleInfo's stylesSizes's item i to defaultStyleInfo's stylesSizes's item 1
	set styleInfo's stylesColors's item i to {0, 0, 0}
end repeat

set the clipboard to (sttr's RecordToStyledText(styleInfo))

beep 2
here's the applescrit.net forum page where people helped me figure this out: Link

[ # ]
i got this working, here's the script:
Authored by: paulrob on Feb 15, '04 07:32:43AM
This is excellent! Does what it says on the tin! Well, almost. Although you get bolds etc, it isn't plain text - the font is Geneva. For some reason Applescript assigns what you might expect to be a plain text to a Geneva font! See my previous post on this. And the original poster's comments. It's to do with the seed value "x" on the line: -

tell sttr to copy StyledTextToRecord("x" as Unicode text) to defaultStyleInfo
Unfortunately, Applescript assigns this "x" a Geneva font right from the start. Therefore, Gevena becomes the default font for the clipboard. I tried the following as a replacement, based on what the original poster said: -

set seed_char to «class ktxt» of (("x" as Unicode text) as record)
tell sttr to copy StyledTextToRecord(seed_char) to defaultStyleInfo
but now the handler throws up an error claiming the clipboard has non-styled text. Sounds like a bit of work on the handler might straighten this out, and then you would have "real" plain text with style info, but not font.

[ # ]
to save the Cmd V step
Authored by: Safar on Feb 12, '04 02:27:43AM

tell application "Finder" to set frontApp to (name of 1st process whose frontmost = true)
tell application frontApp
	activate
	set clip to «class ktxt» of ((the clipboard as text) as record)
	tell application "System Events" to keystroke clip
end tell
and when the script is called, it will directly paste the content of the clipboard as text. I think the script should be saved as a compiled scripted, not an application, but i'm not sure

[ # ]
to save the Cmd V step
Authored by: Safar on Feb 12, '04 03:48:48AM
actually my former script behaves most strangely on my French mac. This one is better

pbpaste | pbcopy
 osascript -e 'tell application "System Events" to keystroke "v" using {command down}'


[ # ]