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)
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.
@ 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.
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.
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
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"})
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!
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.
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"}
*)