0

At this point, I'm sure this is something simple that I'm missing but I can't for the life of me figure it out.

Working on an InDesign script that takes text passed into the script and writes it into the currently selected text area.

insertButton.onClick = function(){
    var postIndex = postList.selection.index;

    var postContent = posts[postIndex].content.rendered;

    $.writeln(app.selection[0].parentStory.contents);

    app.selection[0].parentStory.contents = postContent;

    $.writeln(app.selection[0].parentStory.contents);

    myWindow.close();
}

I've confirmed that the function is getting called correctly, that postContent exists and is what I expect it to be and that the first writeln call dumps out the current value of the TextArea. The second $.writeln never fires, so I know the error is on

app.selection[0].parentStory.contents = postContent;

Is there an updated way to set TextArea contents that I haven't found in documentation?

Thanks in advance!

Keanan Koppenhaver
  • 484
  • 3
  • 8
  • 17

3 Answers3

1

I think your problem is that your window is modal thus preventing any interaction with inDesign objects. You have to quit the dialog first in order to modify objects:

var w = new Window('dialog');
var btn = w.add('button');
btn.onClick = function() {
 w.close(1);
}
if ( w.show()==1){
 //"InDesign is no longer in modal state. So you can modify objects…")
}
Loic
  • 2,173
  • 10
  • 13
0

...postContent exists and is what I expect it to be...

Indesign expects a string here --> is it what you expect as well?

What is an input selection? text? textFrame?

You could

alert(postContent.construction.name)

to ensure what you've got

Jarek

Cashmirek
  • 269
  • 1
  • 9
0

When debugging was enabled in ExtendScript Toolkit, I was able to find the error being thrown:

"cannot handle the request because a modal dialog or alert is active"

This was referring to the dialog I opened when I initiated the script.

Delaying the text insertion until the dialog has actually been closed fixed the issue.

   insertButton.onClick = function(){
        var postIndex = postList.selection.index;

        postContent = posts[postIndex].content.rendered;

        postContent = sanitizePostContent(postContent);
        // The 1 here is the result that tells our code down below that
        // the window has been closed by clicking the 'Insert' button
        myWindow.close(1);
    }

    var result = myWindow.show();

    // If the window has been closed by the insert button, insert the content
    if (result == 1) {
      app.selection[0].parentStory.contents = postContent;
    }
Keanan Koppenhaver
  • 484
  • 3
  • 8
  • 17