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)
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 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:
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!
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.
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. 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.
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.
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…
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!