10.6: Create a 'copy as plain text' service

Sep 23, '09 07:30:04AM

Contributed by: ssawyer

I ran into an issue where I need to copy some items from Numbers into a table in Keynote -- but they kept copying as table cells, thus inserting new cells and messing up my existing table. At first, my only solution was to copy from Numbers, switch to TextEdit, paste-and-match-style (Shift-Command-Option-V), recopy, switch to Keynote, and paste. Not terribly efficient.

With the help of this hint, I realized there was a better way, provided you have GUI scripting turned on. As a bonus, it solves the copy (or paste) as plain text problem for all apps, not just these two

Here's the fix: in Automator, create a new Service, and set it to receive no input and work in any application. Add a Run AppleScript action, and put the following code in the on run routine:

tell application "System Events"
  keystroke "c" using command down
end tell
	
delay 0.05
	
try
  set the clipboard to string of (the clipboard as record)
on error errMsg
  display dialog errMsg
end try
This just presses Command-C to copy, and then converts the clipboard to plain text. The delay is necessary to make sure the clipboard gets populated before the conversion happens. I saved this as Copy Plain Text, and assigned it the Control-Shift-C shortcut.

It's also pretty easy to setup a parallel Paste Plain Text service, with this code:
try
  set oldClipboard to (the clipboard as record)
  set the clipboard to string of (the clipboard as record)
on error errMsg
  display dialog errMsg
end try
	
tell application "System Events"
  keystroke "v" using command down
end tell
	
set the clipboard to oldClipboard
This one is just a little more complicated in order to restore the formatted version of the clipboard after pasting the plain text. You can also set up a keyboard shortcut for this, of course, and it's pretty easy to imagine a "Cut as Plain Text" service as well.

[robg adds: I haven't tested these.]

Comments (7)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20090905230902190