Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

[JS, Win/Mac] String Variable to Clipboard?

Engaged ,
Sep 01, 2016 Sep 01, 2016

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

TOPICS
Scripting
2.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Sep 05, 2016 Sep 05, 2016

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

...
Translate
People's Champ ,
Sep 02, 2016 Sep 02, 2016

Definitively working for me on Mac OS 10.11.6

Loic

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 02, 2016 Sep 02, 2016

Thanks, Loic, for testing the Mac part. It works for me either.

The Windows part is not working. Any ideas?

Tobias

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Sep 02, 2016 Sep 02, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 05, 2016 Sep 05, 2016

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:

Screen.JPG

»Object needed«. What's wrong?

Thanks

Tobias

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Sep 05, 2016 Sep 05, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 05, 2016 Sep 05, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 05, 2016 Sep 05, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 05, 2016 Sep 05, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Sep 05, 2016 Sep 05, 2016

Looks like (as we all could have presumed), this question was already asked and answered :

Re: Script to examine clipboard before pasting

Loic

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 05, 2016 Sep 05, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Sep 05, 2016 Sep 05, 2016

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()?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Sep 05, 2016 Sep 05, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 05, 2016 Sep 05, 2016

We all love complications – don't we?

;o)

Tobias

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 05, 2016 Sep 05, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Sep 05, 2016 Sep 05, 2016
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines