0

I'm having a little problem with an JS script in InDesign. I want to place an empty rectangle when clicking on a button. The creation outsite the onClick()-function works fine, but when I copy the exact code inside the onClick, it doesn't work.

main();
function main()
{
    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;

    //Window
    var dlg = new Window('dialog', "Window");     
    dlg.size = [250,150];

    //Add button to window
    var myButtonGroup = dlg.add ("group");
    myButtonGroup.orientation = "column";
    var search = myButtonGroup.add ("button", undefined, "Do!");

    // Totally works fine 
    var doc = app.activeDocument;
    var rect = doc.rectangles.add({geometricBounds : [0,0,32,100], fillColor : doc.swatches.item("None"), strokeColor : doc.swatches.item("None")});   


    //onClick event
     search.onClick = function() {
        alert("working");
       //Alert is working, code below not 
       var doc = app.activeDocument;
       var rect = doc.rectangles.add({geometricBounds : [0,0,32,100], fillColor : doc.swatches.item("None"), strokeColor : doc.swatches.item("None")});   
     }

    dlg.show();
}

I'm new to Indesign JS programming, so please be indulgent :) I am using InDesign CC 2020 and Javascript.

Best regards Alexander

RobC
  • 22,977
  • 20
  • 73
  • 80
Alex
  • 11
  • 3

1 Answers1

1

Ok, it worked when I add #targetengine "session" and create a 'palette' instead of 'window'.

Why? Don't ask me haha!

Best regards Alex

Alex
  • 11
  • 3
  • Thank you for your finding. I think a 'window' dialog can't change a document state until the 'window' was closed. Any changes within the documents can be done only after you click 'OK' or close the 'window' somehow. Not like a 'palette' that can change a document state any time. My guess. – Yuri Khristich Oct 05 '20 at 09:26