Search code examples
adobe-indesignextendscript

InDesign CC 2017 ExtendScript - Can't overwrite text in TextArea


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!


Solution

  • 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;
        }