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

How to copy a JS variable to the clipboard

Explorer ,
Oct 22, 2008 Oct 22, 2008
I have a script that determines the distance between two guides.

I would like that variable to be copied to the clipboard so that a user can enter it into the control panel if one wishes.

The copy() function will only copy something that is selected in the ID file itself.

Any hints on how to start?

Thanks,
Tom
TOPICS
Scripting
2.9K
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
Community Expert ,
Oct 23, 2008 Oct 23, 2008
Create a text frame and change it's contents to that of the variable, then select it and copy and then delete the text frame. I haven't tested it, so I can't guarantee it would work, though.
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
Explorer ,
Oct 23, 2008 Oct 23, 2008
I thought of that. I am hoping there is something more simple.

Another inelegant solution is to create a prompt where the default is the variable. The user can then copy the number from the prompt.

Tom
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
Community Expert ,
Oct 23, 2008 Oct 23, 2008
That's actually not such a bad idea. Another option I thought about is to write it out to a text file and then launch it. That way the user can also save the value for later use.
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
Explorer ,
Oct 23, 2008 Oct 23, 2008
Hi Tom, try67,

You could also use doScript to call a platform script to do it. VBScript and AppleScript both have ways of manipulating the system Clipboard. It's not particularly elegant, but it's another approach.

Thanks,

Ole
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
Explorer ,
Oct 25, 2008 Oct 25, 2008
Hello,

I took up try67's suggestion because I cannot find any property that will copy to the clipboard.

The following works fine...for one number! A five digit number with a decimal (or even just two digits) overflows the text box. Text that overflows cannot be selected.

I think that the easiest solution is to make the pointSize of the text really small but for the life of me I cannot decipher how to do that.

Any suggestions on adding one or two lines to solve this? I figure the best place to put this property is in the frameProps property record.

Tom

var a = 7;

numToCopy(a);
function numToCopy(numToCopy){
var frameProps = {contents:numToCopy.toString()}
var myTextFrame = app.activeDocument.textFrames.add(frameProps);
myTextFrame.words.everyItem().select();
app.copy();
myTextFrame.select();
myTextFrame.remove();
}//end function numToCopy
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
Participant ,
Oct 25, 2008 Oct 25, 2008
Change the selection line to:

myTextFrame.parentStory.texts[0].select();

or:

app.select(myTextFrame.parentStory.texts[0]);

That way, the capacity of the text frame is irrelevant.

Dave
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
Explorer ,
Oct 26, 2008 Oct 26, 2008
Thanks Dave, that works!

But why does it work? What is it about parentStory and texts[0] that allows the entire contents of myTextFrame to be selected?

I earlier assumed that overflowing text cannot be selected (either manually or by script), but that is not true. Select All will select all text, even the invisible text. The problem I was having is when not even one character is visible because the 5 or 6 digit number is just too big for a small text frame. In that case, one cannot even put an insertion point in the text frame, which is necessary when manually trying to use the Select All tool.

Tom
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
Participant ,
Oct 26, 2008 Oct 26, 2008
LATEST
The point is that the parentStory is the story that flows through the text frame. The "entire contents" of the text frame is just the text visible in the text frame. You can't select a story, but you can select all the text of a story, hence the use of the texts[0] property of the story.

Dave
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