Convert clipboard to plain text

I’m new to AppleScript and want something that strips all formatting from text in the clipboard. I’ve seen a few posts on this, from which I gleaned the following:

set the clipboard to «class ktxt» of ((the clipboard as text) as record)

Problem is, when I paste into, say, an Outlook message, the text is formatted as Helvetica. Pasting into Word its Geneva.

Ideally, I’d want the clipboard’s contents to be equivalent to if I’d copied it from a TextEdit window having pressed SHIFT+CMD+T (Format > Make Plain Text).

Some of the solutions involved scripting for for a specific application (choosing the “Paste and Match Style” menu options) but I want this to work everywhere. Possible?

Thanks
Stuart

Model: MacBook Pro
AppleScript: 2.2.1
Browser: Chrome 21.0.1180.82
Operating System: Mac OS X (10.7)

Why can’t you use paste and match style?

Isn’t that the correct font if you paste and match style into that documents of said applications?

Which applications do you use that doesn’t support paste and match style?

:slight_smile:

Thanks for your response.

No. In Outlook it’s Calibri and in Word it’s Cambria.

Good question. Perhaps there are none. But in the script I don’t want to have to target a specific application. Do I have to?

How about ?

the clipboard as text

Isn’t that what I’ve tried? See my initial post.

It is converting it to plain text. Copy formatted text from word and then run this:

set the clipboard to (the clipboard as text)
return the clipboard as record

Unfortunately it doesn’t work. In Outlook it still appears as Helvetica; in Word, Geneva.

I think that has to do with the defaults of the programs, not the type of text in your clipboard. For example, type some text in Text Edit, copy it, and run

the clipboard as record

Now, paste that text into Word.

I only get that if I have TextEdit in plain text mode. By default it isn’t, and if I run that script this is what appears in the Results pane:

Perhaps the question is, how can I convert the clipboard’s contents from the above into this:

The point of the excercise was to show that even if you have plain text in your clipboard, you will get the same results when you paste it into Word and Outlook.

By using the first script I posted:

set the clipboard to (the clipboard as text)
return the clipboard as record

Here is the clipboard from a rich text document in TextEdit:

Here is the clipboard after I run the script:

Hello!

This should work.



set mytekxt to (the clipboard as text)
set l to length of mytekxt

tell application "System Events"
	
	set ap to name of (every application process whose frontmost is true and visible is true) as text
end tell
tell application ap to activate
tell application "System Events"
	tell application process ap
		repeat with i from 1 to l
			keystroke (character i of mytekxt)
		end repeat
	end tell
end tell

Please don’t say anything about coercions or anything! :smiley:

Doesn’t

set mytekxt to «class ktxt» of ((the clipboard as text) as record) as text

do the same thing as …

set mytekxt to the clipboard

???

Paste some formatted text onto the clipboard and try! :slight_smile:

I don’t get the same results. When I have “true” plain text in the clipboard (i.e. copied text from TextEdit in plain text mode), it pastes into Outlook/Word/whatever respecting whatever font is selected in that application. I’m afraid that your script doesn’t achieve the same effect.

set the clipboard to «class ktxt» of ((the clipboard as text) as record) as text
the clipboard as record

and

set the clipboard to (the clipboard as text)
the clipboard as record

produce the same result for me

It seems so, and I am not spending more time on this, for my approach circumvents the clipboard anyway.

The problem here, is that some apps seems to have a memory of what has been on the clipboard before, so I suspect it to work, like adding default values to a record, that doesn’t have those properties from before.

Those apps use, and manipulate the attribute runs, so, they might not take the clipboard for exactly what it is! (I hope that gave sense).

Edit

It is updated, and noted, that an example changed. :wink: As it for my purpose is not relevant, but I do think some applications treat the clipboard, as just an instance in a stack or queue like datastructure, and in such an environment, it could make a difference really, as long as applescript isn’t smart enough to short circuit the coercions.

Edit++

It just strikes me, that maybe, the place to coerce the clipboard, is within an application tell block, that it may lead to better results, if the event isn’t sent right out of the tell block then.

I see that now, I must have missed it earlier. The text from my script is coming into word as Geneva.

It is a solution I believe to work in post 11 if you want something faster, then I can’t help you. Because the next step is to go in and meddle with the attribute runs of the rtf on the clipboard,

And even then, as some apps choose to interpret the clipboard on their own! You aren’t really guarranteed the results you opted for. :smiley:

McUsr, how can I test your script without running it from the AppleScript window? I’ve tried saving it as an app to launch via Automator but it isn’t working. I guess there’s a simpler approach to attach it to a key combo but I’m new this…

Thanks
Stuart

I’m sorry, I do take things for granted!

I intended it to be used from the script menu, if you don’t have a script menu installed, then you have to activate it in the preferences of Applescript editor.

you can then open a script folder, and drag the icon of the folder onto the save as sheet, and save it there. I have a clipboard folder myself, this one is now named PasteMatchStyle.

Please check that it works when run from the script menu. and having the word or whatever doc in the foreground.

Should there be any trouble with stuffing the script into the run applescript item in automator and create a service of it, that you can assign a short cut key to, please do come back! :slight_smile:

I really hope it works for you.