Copy link to clipboard
Copied
----------------------------------------------------
Main Issue & Details
----------------------------------------------------
Adobe Illustrator CC 2018 — Javascript
The sample code below:
- Opens a dialog window with an input field for multi-line text.
- After clicking "OK", a new text frame is generated with the contents from the input field.
- Displays alert message with # of number items selected
It is actually selected because I can immediately move the text box with my keyboard arrow keys after it is generated, but for some reason, it is not being read as being selected by the script for some reason, which prevents me from continuing with the rest of my script.
When tested with alert( slct.length ); like in the example code below, it returns a value of 0, even though it is selected, as I've mentioned.
**EDIT (May 25, 2018)**
Adding a couple screenshots for reference.
The dialog box with the multi-line text input:
After clicking "OK", the text frame is generated, and clearly selected—iindicated by the selection around the text frame on the top left (if you can see it) but also from the selection indicators in the layers panel—yet alert( slct.length ); returns a value of 0. So trying to do anything else with the script that requires a current selection does not work:
**END EDIT**
----------------------------------------------------
Things I've tried
----------------------------------------------------
In the script below, I have a redundant selection just to make sure it is selected, but it doesn't work. I've tried both methods individually as well, but just left them both here in the code so you guys can see.
I tried throwing in app.redraw(); at different places in the code as well to see if that would help, but didn't seem to work.
I usually have been able to select text frames and objects with .selected = true or attaching an attribute note and selecting it looping through all items in the document to select anything with the note. And it gets read by the script and can continue on.
Again, both methods do actually end up with the item selected, but for some reason, neither methods seem to be read as selected by the script in this scenario, so I can't continue with anything else in the script afterward. So possibly a problem specific to text generated from a dialog window input field / ScriptUI.
I imagine I'm missing something very simple. Hope that is the case, anyway. Would greatly appreciate anyone taking the time to look into this and provide any solutions or suggestions!
----------------------------------------------------
Sample Code
----------------------------------------------------
if( app.activeDocument ){
var doc = app.activeDocument;
var slct = doc.selection;
// Create the Dialog Window:
var dialogWindow = new Window( "dialog", "Generate Text from Input" );
// Input Text Box Title
var inputTextBoxTitle = dialogWindow.add( "group" );
inputTextBoxTitle.alignment = "left";
inputTextBoxTitle.add( "statictext", undefined, "Generate Text from Input:" );
// Input Text Box
var inputTextBox = dialogWindow.add( "edittext", [0, 0, 300, 100], "Lorem ipsum line 1.\nLorem ipsum line 2.\nLorem ipsum line 3.", {multiline: true, wantReturn: true} ); // [ Left, Top, Width, Height ]
inputTextBox.active = true;
// Button Group
var buttonGroup = dialogWindow.add( "group" );
buttonGroup.alignment = "right";
buttonGroup.add( "button", undefined, "OK" );
buttonGroup.add( "button", undefined, "Cancel" );
// If user clicks "OK", generate new text frame from input text:
if (dialogWindow.show () == 1) {
var generatedText = doc.textFrames.add();
generatedText.contents = inputTextBox.text;
generatedText.top = 0;
generatedText.left = 0;
generatedText.selected = true;
generatedText.note = "GENERATED TEXT";
app.redraw();
}
// Redundant selection, just to make sure:
for( var i = 0; i < doc.pageItems.length; i++ ){
if( doc.pageItems.note === "GENERATED TEXT" ){
doc.pageItems.selected = true;
}
}
// Display alert message with # of items selected:
alert( "# of items selected (app.activeDocument.selection.length) = " + slct.length );
}
Message was edited by: Author
1 Correct answer
Turns out I was wrong on the whole show() thing!
The real matter appears to be in that when you make the selection into a variable, it looks like it makes a copy of the selection object at that time, but is a separate deal and does not update with changes.
Okay, so it doesn't matter if your alert is in the show() block, what matters is that you always refer to it as doc.selection instead of a variable!
Explore related tutorials & articles
Copy link to clipboard
Copied
Because your code is going to run in different blocks, such as one being run in the dialog window after all the initial code is ran.
Therefore you will need to move the alert line into the show() {} block.
Right now you are thinking you're doing the "Redundant selection" but it really does nothing as at the time of that execution there's no items in your document.
Copy link to clipboard
Copied
Hey Silly-V ,
Thanks so much for taking the time to look at this and replying. I've learned a lot from your replies throughout the forums.
Unfortunately, that doesn't work either for me. I forgot to mention it as one of the things I've already tried. And just tried again now just to make sure, but even with the alert(slct.length); inside the show(){} block, it still returns = 0 for me. If I try to do anything else that requires an active selection, it won't run.
Unless I'm doing something wrong here, but I don't see a mistake.
Running this in Adobe Illustrator CC 2018. Let me know if you get a chance to test the code and find that it actually works for you instead. Totally understandable if you don't have time to test it yourself, though. Feel free to throw out any other suggestions that I could try.
Thanks again.
--------------------------------------
Revised Code Tested Below
--------------------------------------
if( app.activeDocument ){
var doc = app.activeDocument;
var slct = doc.selection;
// Create the Dialog Window:
var dialogWindow = new Window( "dialog", "Generate Text from Input" );
// Input Text Box Title
var inputTextBoxTitle = dialogWindow.add( "group" );
inputTextBoxTitle.alignment = "left";
inputTextBoxTitle.add( "statictext", undefined, "Generate Text from Input:" );
// Input Text Box
var inputTextBox = dialogWindow.add( "edittext", [0, 0, 300, 100], "Lorem ipsum line 1.\nLorem ipsum line 2.\nLorem ipsum line 3.", {multiline: true, wantReturn: true} ); // [ Left, Top, Width, Height ]
inputTextBox.active = true;
// Button Group
var buttonGroup = dialogWindow.add( "group" );
buttonGroup.alignment = "right";
buttonGroup.add( "button", undefined, "OK" );
buttonGroup.add( "button", undefined, "Cancel" );
// If user clicks "OK", generate new text frame from input text:
if (dialogWindow.show () == 1) {
var generatedText = doc.textFrames.add();
generatedText.contents = inputTextBox.text;
generatedText.top = 0;
generatedText.left = 0;
generatedText.selected = true;
generatedText.note = "GENERATED TEXT";
app.redraw();
// Display alert message with # of items selected:
alert( "# of items selected (app.activeDocument.selection.length) = " + slct.length ); // Still returns = 0
}
}
Copy link to clipboard
Copied
Turns out I was wrong on the whole show() thing!
The real matter appears to be in that when you make the selection into a variable, it looks like it makes a copy of the selection object at that time, but is a separate deal and does not update with changes.
Okay, so it doesn't matter if your alert is in the show() block, what matters is that you always refer to it as doc.selection instead of a variable!
Copy link to clipboard
Copied
You're right! I didn't realize that's what happens when you make a variable for app.activeDocument.selection.
Crazy. Funny thing is that I wrote out the full "app.activeDocument.selection.length" in the alert message itself but still used the variable to call on it. Didn't think to try writing it all out instead of using the variable.
Thanks again so much, Silly-V ! Always got the answers.
------------------------------
Working Solution Code
------------------------------
if( app.activeDocument ){
var doc = app.activeDocument;
var slct = doc.selection;
// Create the Dialog Window:
var dialogWindow = new Window( "dialog", "Generate Text from Input" );
// Input Text Box Title
var inputTextBoxTitle = dialogWindow.add( "group" );
inputTextBoxTitle.alignment = "left";
inputTextBoxTitle.add( "statictext", undefined, "Generate Text from Input:" );
// Input Text Box
var inputTextBox = dialogWindow.add( "edittext", [0, 0, 300, 100], "Lorem ipsum line 1.\nLorem ipsum line 2.\nLorem ipsum line 3.", {multiline: true, wantReturn: true} ); // [ Left, Top, Width, Height ]
inputTextBox.active = true;
// Button Group
var buttonGroup = dialogWindow.add( "group" );
buttonGroup.alignment = "right";
buttonGroup.add( "button", undefined, "OK" );
buttonGroup.add( "button", undefined, "Cancel" );
// If user clicks "OK", generate new text frame from input text:
if (dialogWindow.show () == 1) {
var generatedText = doc.textFrames.add();
generatedText.contents = inputTextBox.text;
generatedText.top = 0;
generatedText.left = 0;
generatedText.selected = true;
generatedText.note = "GENERATED TEXT";
app.redraw();
}
// Display alert message with # of items selected:
alert( "# of items selected (app.activeDocument.selection.length) = " + app.activeDocument.selection.length ); // Returns value = 1.
}
Copy link to clipboard
Copied
To add to the correction to my previous snafu: the code featuring modal dialogs holds the script until the show() function passes.

