Copy link to clipboard
Copied
Hello,
I want to set the contents of a string variable to the system clipboard. For Mac it's simple via AppleScript, but how could it work for windows? I searched and read all relevant posts and did some research with google.
Here's my potion of code:
// copy to clipboard
if ( $.os.toLowerCase().indexOf('mac') >= 0 ) {
app.doScript ( 'set the clipboard to item 1 of arguments', ScriptLanguage.applescriptLanguage, [_stringOUT] ); // mac
} else {
app.doScript ( 'echo ' + _stringOUT + ' | clip', ScriptLanguage.visualBasic ); // win
}
A friend of mine tested the Windows part – I'm only working on a Mac –, but it doesn't work. Can someone please help me figure this out?
Thanks!
Tobias
1 Correct answer
After long investigations I think I finally made it:
_OUT = "Ich bin ein Copy-Paste-Test.";
if ( $.os.toLowerCase().indexOf('mac') >= 0 ) { // widows to clipboard
app.doScript ( 'set the clipboard to item 1 of arguments', ScriptLanguage.applescriptLanguage, [_OUT] ); // mac
} else {
app.doScript ( 'Dim theObject\r Set theObject = CreateObject (\"htmlfile\")\r Call theObject.ParentWindow.ClipboardData.SetData(\"text\",\"'+_OUT+'\")', ScriptLanguage.visualBasic ); // win
}
Can somebody please
...Copy link to clipboard
Copied
Definitively working for me on Mac OS 10.11.6
Loic
Copy link to clipboard
Copied
Thanks, Loic, for testing the Mac part. It works for me either.
The Windows part is not working. Any ideas?
Tobias
Copy link to clipboard
Copied
No windows at hand but seeking googloo gives this back:
For once it's in french but the code behind should be understandable.
https://msdn.microsoft.com/fr-fr/library/e2z694z2(v=vs.100).aspx
Copy link to clipboard
Copied
Thanks for this hint, but my knowledge of VBScript is too small – with this code:
_OUT = "I'm a copy paste test text ...";
if ( $.os.toLowerCase().indexOf('mac') >= 0 ) { // widows to clipboard
app.doScript ( 'set the clipboard to item 1 of arguments', ScriptLanguage.applescriptLanguage, [_OUT] ); // mac
} else {
app.doScript ( 'My.Computer.Clipboard.SetText(arguments)', ScriptLanguage.visualBasic, [_OUT] ); // win
}
I got the following error:
»Object needed«. What's wrong?
Thanks
Tobias
Copy link to clipboard
Copied
Did you try with a basic line at first to test if it's the instruction or the way you are using it which is problematic ?
app.doScript ( 'My.Computer.Clipboard.SetText("Does this work ?")', ScriptLanguage.visualBasic );
Loic
Copy link to clipboard
Copied
Yes, I tried – same error.
I tried even this:
app.doScript ( 'My.Computer.Clipboard.SetText("Test")\n', ScriptLanguage.visualBasic );
because I read, that VB is very strict with line breaks. Still »object needed«.
Tobias
Copy link to clipboard
Copied
I read this:
Clipboard.SetText-Methode (String) (System.Windows.Forms)
and tried:
app.doScript ( 'System.Windows.Forms.Clipboard.SetText("Test")\n', ScriptLanguage.visualBasic );
and:
app.doScript ( 'Clipboard.SetText("Test")\n', ScriptLanguage.visualBasic );
Same error all the time.
Tobias
Copy link to clipboard
Copied
After long investigations I think I finally made it:
_OUT = "Ich bin ein Copy-Paste-Test.";
if ( $.os.toLowerCase().indexOf('mac') >= 0 ) { // widows to clipboard
app.doScript ( 'set the clipboard to item 1 of arguments', ScriptLanguage.applescriptLanguage, [_OUT] ); // mac
} else {
app.doScript ( 'Dim theObject\r Set theObject = CreateObject (\"htmlfile\")\r Call theObject.ParentWindow.ClipboardData.SetData(\"text\",\"'+_OUT+'\")', ScriptLanguage.visualBasic ); // win
}
Can somebody please test this bit of code on a windows machine?
Thanks!
Tobias
Copy link to clipboard
Copied
Looks like (as we all could have presumed), this question was already asked and answered :
Re: Script to examine clipboard before pasting
Loic
Copy link to clipboard
Copied
After digging in the forums and google I found this too and it gave the hint, which way to go. But that was »read the clipboard«. »Write to clipboard« took me a while to figure out – it's not just »setText« instead of »getText«.
Tobias
Copy link to clipboard
Copied
It works on Windows 7 64-bit.
But:
Since it seems to build an HTML object, and then use the HTML to copy the text,
and,
since it anyway requires InDesign (app.doScript belong to the ID dom)
wouldn't it be just as easy to create a text frame in InDesign, add the text, and then do a copy()?
Copy link to clipboard
Copied
wouldn't it be just as easy to create a text frame in InDesign, add the text, and then do a copy()?
why make it simple when we can make it complicated
Copy link to clipboard
Copied
We all love complications – don't we?
;o)
Tobias
Copy link to clipboard
Copied
Thanks for testing. So I'll use it.
Yes, I could have used adding text frame, paste, copy, deleting text frame. But I'd like to avoid changing the ID file, because the user could use undo in the process of the script (from which this code is an excerpt). And a appearing and disappearing text frame may cause irritations ...
Tobias
Copy link to clipboard
Copied
You can create a temp invisible document on the fly and discard it after copy but it's true it's a few DOM manipulations

