Convert clipboard to plain text

McUsr,

System Events does not need to keystroke characters. It can keystroke the variable itself.

tell application "System Events" to keystroke mytekxt

Also, a better approach for launching “keystroke” applications is to save the script as an Application and change the plist so that it runs in the background. Then launch the application with Spotlight. (command space)

A little delay doesn’t hurt! :wink:

tell application "System Events" to keystroke mytekxt

Thanks! :slight_smile: That is a good one, but I haven’t tried it before, I’ll check it out at my leisure!

(I usually just execute my scripts with Quicksilver.)

I was with you until you said:

I guess you’re talking about adding my new script to the script menu. Can’t figure out how to do that. Feeling stoopid.

Until Nigel, Stefan, Shane, Adam, etc. chime in on the proper way to put the text into the clipboard, this should work for typing.

set mytext to the clipboard
tell application "System Events" to keystroke mytext

Save that as an application.
Control Click the application and Show Package Contents
Open the Info.plist and add this above the last tag.

LSBackgroundOnly

Copy the text into the keyboard, select the window in which you want it typed, and command space to launch spotlight and select the application name.

Well, I meant that when you save it, in order to not have to navigate to the script folder, that when you have the saveas dialog open, in script editor:

  • Open a folder from the script menu by the top item, and drag the “proxy icon of the folder” onto the top of the scripts menu, util you see the green “+” then you just drop it there, and you’ll notice that the current directory of the save as dialog has changed.

That was what I meant. :slight_smile:

@ adayzdone A general good working solution, would be interesting to see, but I am afraid, that since it is applications that has their quirks, such solutions don’t work.

And if you think your solution will work at all times, then use it. But I know, that even mine, may break, if the app is to busy with stuff at the moment my routine, that is slightly slower than yours, but doesn’t use a real delay, starts to pound in characters.

The repeating of the insertion of charcters makes the process into a constant time thing, not so if somebody uses the script and has a block of text on the clip board with a size of say 1k or more, then your solution may fail, just for the size of it, whereas mine will type in the text steadfastly.

Do you mean this?

I don’t appear to be able to drag a folder onto that script menu.

@adayzdone’s solution is working for me but I’d like to get yours working too :slight_smile:

Hello!

You have to click the menu item, and then drag the proxy icon of that folder.

I am glad you have something working!

Sorry. I don’t understand what the goal is here. If I did I might be able to work out what you’re describing to me.

I have the script saved under a folder in my Documents folder. I think you’re telling me how to make this visible in the scripts menu. Are you?

And I very much appreciate your continued patient help.

This is the problem. Scripts saved in (~)/Library/Scripts are displayed automatically in the Script Menu

Stefan,

Is there a way to avoid “keystroking” by placing the text into the clipboard properly?

The clipboard contains normally several representations of objects, in case of text it can contain plain text (MacRoman), UTF8, UTF16 (Unicode text), RTF and probably others.

Each application can treat pasting text in it’s own way. So I guess sending keystrokes is the most reliable way for versatility.

It was. Dragged it in and it appears and what’s more it works!

Many many thanks to you all for your continued help today.

Stuart

You have to get the script into the folder “your home folder:Library:Scripts:somewhere” in order to make it visible in the scripts menu.

You are welcome. :slight_smile:

Hello

When we use
set the clipboard to the clipboard as text, the container stores in fact :

{Unicode text:“azertyu”, string:“azertyu”, scrap styles:«data styl01000000000010000E00030000000C00000000000000», «class utf8»:“azertyu”, «class ut16»:“azertyu”}

Happily, there is a small free application which really drop extraneous cllipboard contents.

It’s « Plain Clip » available from :
http://www.bluem.net/en/mac/plain-clip/

Yvan KOENIG (VALLAURIS, France) mardi 28 août 2012 23:24:55

Hello!

Thanks Yvan! :slight_smile:

I had rejected that little app for a long time ago, seeing no purpose in it.

Here is a script using that little gem

I have called it: TotallyPlainText!


set tcmd to first paragraph of (do shell script "mdfind -name \"Plain Clip.app\"")

set tcmd to tcmd & "/pc"

do shell script quoted form of tcmd

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
		keystroke "v" using command down
	end tell
end tell


This is another option, using ASObjC Runner:

script textOnlyClip
	set aString to current application's NSApp's passedValue() as text -- get passed text
	set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
	tell pb to clearContents() -- clear pb
	tell pb to writeObjects_({aString}) -- write text
end script

tell application "ASObjC Runner" to run the script {textOnlyClip} passing "Hello world"
clipboard info
--> {{«class utf8», 11}, {«class ut16», 24}, {string, 11}, {Unicode text, 22}} -- no scrap styles

If you’re saving as a Cocoa-AppleScript app it would simply be:

set pb to current application's NSPasteboard's generalPasteboard()
pb's clearContents()
pb's writeObjects_({"some text"})

Hello Shane!

That was an interesting example!

So, now I have also learned something about passing arguments between

Now I my wondering about creating a record not so vanilla, or maybe, and send it back to the clipboard without scrapstyles! But I am going to let it be! :slight_smile:

I suspect scrap styles is a legacy thing that no-one has got around to removing from the code for the clipboard commands – I can’t see that it achieves anything but problems these days. Someone should log a bug on it.

Hello

I already tried to do that but the system automatically added the extraneous items.


set theRecord to the clipboard as record
(*
{«class rtfd»:«data rtfd72746664000000000300000002000000070000005458542E727466010000002E780100002B00000001000000700100007B5C727466315C616E73695C616E7369637067313235325C636F636F61727466313138370A5C636F636F6173637265656E666F6E7473317B5C666F6E7474626C5C66305C6673776973735C6663686172736574302048656C7665746963613B7D0A7B5C636F6C6F7274626C3B5C7265643235355C677265656E3235355C626C75653235353B7D0A5C6465667461623536360A5C706172645C74783536305C7478313132305C7478313638305C7478323234305C7478323830305C7478333336305C7478333932305C7478343438305C7478353034305C7478353630305C7478363136305C7478363732305C7061726465667461623536365C7061726469726E61747572616C0A0A5C66305C66733234205C636630205C6578706E64305C6578706E647477305C6B65726E696E67300A5C757030205C6E6F7375706572737562205C756C6E6F6E65205C6F75746C305C7374726F6B65776964746830205C7374726F6B65633020426C61626C61626C617D010000002300000001000000070000005458542E7274661000000064CB3D50B60100000000000000000000», «class RTF »:«data RTF 7B5C727466315C616E73695C616E7369637067313235325C636F636F61727466313138370A5C636F636F6173637265656E666F6E7473317B5C666F6E7474626C5C66305C6673776973735C6663686172736574302048656C7665746963613B7D0A7B5C636F6C6F7274626C3B5C7265643235355C677265656E3235355C626C75653235353B7D0A5C6465667461623536360A5C706172645C74783536305C7478313132305C7478313638305C7478323234305C7478323830305C7478333336305C7478333932305C7478343438305C7478353034305C7478353630305C7478363136305C7478363732305C7061726465667461623536365C7061726469726E61747572616C0A0A5C66305C66733234205C636630205C6578706E64305C6578706E647477305C6B65726E696E67300A5C757030205C6E6F7375706572737562205C756C6E6F6E65205C6F75746C305C7374726F6B65776964746830205C7374726F6B65633020426C61626C61626C617D», «class utf8»:"Blablabla", «class ut16»:"Blablabla", uniform styles:«data ustl0200000090000000000000001400000020000000010000000900000000000000010000006C000000040000000000000000000000020100000100000000000000050100002C000000646D616E2400000001000000040000000100000000000000000000000900000048656C766574696361000000060100000400000000000C0007010000060000000000000000000000», string:"Blablabla", scrap styles:«data styl0100000000000E001100150000060C00000000000000», Unicode text:"Blablabla"}
*)
set theText to the clipboard as text
--> "Blablabla"
set newRecord to {string:theText}
(*
{string:"Blablabla"}
*)
set the clipboard to newRecord
(*
{string:"Blablabla"}
*)
delay 0.5
the clipboard as record
(*
{Unicode text:"Blablabla", string:"Blablabla", scrap styles:«data styl01000000000010000E00030000000C00000000000000», «class utf8»:"Blablabla", «class ut16»:"Blablabla"}
*)

Yvan KOENIG (VALLAURIS, France) mercredi 29 août 2012 09:59:37