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

An AppleScript to convert the clipboard to plain text System
Trying to convert RTF-formatted text on the clipboard to plain text is a nightmare. There are many solutions out there, some of which cost money, and others which are free but don't work reliably. So here's an AppleScript that will do the job:
try
  set the clipboard to string of (the clipboard as record)
on error errMsg
  display dialog errMsg
end try
Turn it into a run-only AppleScript, bind that AppleScript to a keyboard shorcut with a utility like Keyboard Maestro (my personal favorite), and never look back again. Note that the code above is adapted from the script "clip2pure_text" found at the bottom of this blog post.

[robg adds: In the comments to this older hint, an alternative method was provided -- just do a pbpaste | pbcopy, if you happen to be working in Terminal when you need this done.]
    •    
  • Currently 2.43 / 5
  You rated: 2 / 5 (7 votes cast)
 
[16,316 views]  

An AppleScript to convert the clipboard to plain text | 18 comments | Create New Account
Click here to return to the 'An AppleScript to convert the clipboard 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 the clipboard to plain text
Authored by: hamarkus on Sep 05, '07 02:37:43PM
I've been a happy camper for quite some time now with:
http://www.bluem.net/downloads/plain-clip_en/
I also launch it via a keyboard shortcut (cmd-alt-shift-P in my case).

[ Reply to This | # ]
An AppleScript to convert the clipboard to plain text
Authored by: mistersquid on Sep 06, '07 06:55:44AM

Plain Clip is one of those solutions that doesn't work reliably. After it executes once successfully, it often will have no effect on subsequently copied RTF-formatted text, a condition that can be solved only by logging out and then back in.



[ Reply to This | # ]
An AppleScript to convert the clipboard to plain text
Authored by: Peter Kappesser on Sep 05, '07 03:29:13PM

What I'd like is something that gives contextual menu items for "Copy without style" and "Paste without style" -- anyone know if something like this is available?



[ Reply to This | # ]
Contextual Menu
Authored by: Krioni on Sep 05, '07 08:35:32PM
Yes, there is. Go get OnMyCommand and make a new contextual menu item using the script posted in the hint. Good luck, and enjoy!

---
http://www.danshockley.com

[ Reply to This | # ]

Contextual Menu
Authored by: Krioni on Sep 05, '07 08:48:01PM

Some follow-up to my previous comment. Get OnMyCommand, then use OMCEdit (comes with it) to make a new command. Call it "Paste without Style" (no quotes), choose AppleScript for the Execution Mode, check the box that says "Paste Clipboard after Execution," then paste the script from the original hint in the text area labeled Command. Click the Save button.

That should be it. The default place for OnMyCommand contextual menu items is to be in a submenu of the OnMyCommand menu, but you can choose instead to put it in the First Level by choosing that from the Location popup when making/editing this item in OMCEdit.

Hope that helps. I didn't do Copy without Style because (1) I'm not sure I see the point, since I don't care what's in the clipboard, (2) it's not as quick a comment, since I'd have to tweak the script, and (3) once you've pasted without style once using this method, you've converted the clipboard to plain text, losing any style anyway.

So, not perfect, but gets the biggest part of what's useful.

---
http://www.danshockley.com



[ Reply to This | # ]
Contextual Menu
Authored by: Peter Kappesser on Sep 06, '07 12:18:18AM

Thanks, OnMyCommand is slick and does the trick! (Rob, I nominate it for a Pick of the Week!) And I was able to download a command someone already created, "Paste plain text" which looks to be the pbpaste | pbcopy method.



[ Reply to This | # ]
An AppleScript to convert the clipboard to plain text
Authored by: Stef@nK on Sep 05, '07 11:50:42PM
To avoid losing any Unicode data I would use Unicode text of the record instead of string

[ Reply to This | # ]
An AppleScript to convert the clipboard to plain text
Authored by: mistersquid on Sep 06, '07 07:02:40AM

Thanks for the suggestion which I am going to adopt. I also am going to alter my AppleScript so that it converts the clipboard and pastes. As Krioni notes below, this makes more sense than simply converting the clipboard.

A little OT, I just want to say how much I appreciate MacOSXHints which has been here since the start of OS X. Back in the day, it used to be the only place one could find reliable tips on how to get the UNIX layer working properly. Now, thanks to the community of readers, it is an invaluable resource for improving the random bits of code we all use to keep our systems running in ways that suit our needs, to say nothing of the Picks of the Week, software secrets, and undocumented features.



[ Reply to This | # ]
An AppleScript to convert the clipboard to plain text
Authored by: mistersquid on Sep 06, '07 09:19:25AM

The following AppleScript will transform RTF-formatted contents on the clipboard to plain text and will paste the clipboard into whatever application in which the AppleScript was invoked.

This means that you can use the AppleScript with the launcher/keyboard shortcut utility/accessibility menu of your choice.

Thanks, everyone, for your comments and suggestions.

try
	set the clipboard to Unicode text of (the clipboard as record)
on error errMsg
	display dialog errMsg
end try
tell application "System Events"
	keystroke tab using (command down)
	key code 9 using {command down}
end tell


[ Reply to This | # ]
An AppleScript to convert the clipboard to plain text
Authored by: mistersquid on Sep 06, '07 09:23:21AM

Note: the curly braces and parentheses in the "System Events" code block don't seem to affect the functioning AppleScript. That is, they seem interchangeable.



[ Reply to This | # ]
For the sake of consistency
Authored by: mistersquid on Sep 06, '07 09:49:26AM
try
	set the clipboard to Unicode text of (the clipboard as record)
on error errMsg
	display dialog errMsg
end try
tell application "System Events"
	key code 48 using {command down} --Command-tab
	key code 9 using {command down} --Command-V
end tell


[ Reply to This | # ]
If you want to paste the text into the frontmost application…
Authored by: Stef@nK on Sep 07, '07 07:52:12AM
try this:

try
	set the clipboard to Unicode text of (the clipboard as record)
	tell application "System Events"
		tell process (get 1st process whose frontmost is true)
			keystroke "v" using command down
		end tell
	end tell
on error errMsg
	display dialog errMsg
end try


[ Reply to This | # ]
If you want to paste the text into the frontmost application…
Authored by: mistersquid on Sep 07, '07 10:14:47AM

Did you test the code you proposed?

I've tried my own version of the code you offer as well as your specific code and both do not work.

The failure is due to the fact that the frontmost process ends up being the AppleScript itself! You have to tell System Events to go to the 2nd frontmost process which cannot be done by using "get 2nd process whose frontmost is true".



[ Reply to This | # ]
If you want to paste the text into the frontmost application…
Authored by: Stef@nK on Sep 07, '07 11:28:24AM

Of course I've tested it ;-)
if you start the script from the script menu and not in Script Editor, it should work.



[ Reply to This | # ]
An AppleScript to convert the clipboard to plain text
Authored by: cargoplex on Sep 06, '07 10:40:55AM
I'm embarrassed at how long it took me to find this...

There is actually a paste command built right into OS X that will reformat any text on the clipboard to match the style of the document you are pasting it into.

Under the Edit menu, simply select Paste and Match Style (CMD+OPT+SHIFT+V)!

---
The world sounds better through headphones.

[ Reply to This | # ]

An AppleScript to convert the clipboard to plain text
Authored by: Anonymous on Sep 06, '07 02:53:59PM

I love it when a convoluted hint like this is answered by a simple keystroke.



[ Reply to This | # ]
An AppleScript to convert the clipboard to plain text
Authored by: mistersquid on Sep 06, '07 03:50:03PM

That keystroke combination does not exist in many application contexts. To name a few: Tinderbox, Safari, Skim, EndNote, and Word.

Just because a hint does not apply to you does not mean it is valuable to others.

I love it when users with very little experience deign to critique the value of utilities they have no use for.



[ Reply to This | # ]
An AppleScript to convert the clipboard to plain text
Authored by: avarame on Sep 08, '07 10:25:49AM

Paste into TextEdit, Format->Make Plain Text, copy it back out.



[ Reply to This | # ]