Skip to main content
Known Participant
September 19, 2019
Question

Cancel, stop script

  • September 19, 2019
  • 1 reply
  • 898 views

Hello everyvody

A little help..

I make this simple test script for Illustrator

At this point even if I press the cancel button the script continues to work..

How to make "cancel" button to stop the script. ...?

Thanks,

Nick Riviera

 

var doc = app.activeDocument;
                 
function panel(){
    var unit;  
    var units = ['mm',];   
    var panel = new Window("dialog", "test", undefined, { borderless: false });
        panel.alignChildren = "left"; 
       
    var r0 = panel.add ("group");
    r0.add ('statictext {text: "unidades", characters: 8, justify: "left"}');
    var dims = r0.add("dropdownlist", undefined, units);
    dims.selection = dims.items[0];
   
    var r1 = panel.add ("group");
    r1.add ('statictext {text: "x", characters: 8, justify: "left"}');
    var w = r1.add("edittext",undefined,"100");
    w.characters = 10;
    
    var r2 = panel.add ("group");
    r2.add ('statictext {text: "h", characters: 8, justify: "left"}');
    var h = r2.add("edittext",undefined,"50");
    h.characters = 10;
    
    var r6 = panel.add ("group");
    r6.add("button", undefined, "OK");
    r6.add("button", undefined, "Cancel");
    
     unit = dims.selection.text;  
     x = Number(new UnitValue(w.text + " " + unit).as('pt'));
     y = Number(new UnitValue(h.text + " " + unit).as('pt'));
    
    
    panel.show(); 
}

function cR(){
   doc.rulerOrigin = [0,0]; 
   var layer2 = doc.layers.add();
                layer2.name = "test";
                rectangle = layer2.pathItems.rectangle(x, 0, y, x);
}
    
var rS = panel();
cR(rS);

 

 

This topic has been closed for replies.

1 reply

Silly-V
Legend
September 19, 2019

Where you have "show()" inside your panel function, change it to:

if (panel.show() == 2) {

  return null;

} else {

     unit = dims.selection.text;  
     x = Number(new UnitValue(w.text + " " + unit).as('pt'));
     y = Number(new UnitValue(h.text + " " + unit).as('pt'));

  return { x : x, y : y };

}

When the Cancel button or Escape is pressed in a dialog it always returns a number 2. Anything else means something was pressed other than a Cancel button. Therefore, if the show() function returns a 2, return a null or a false value to let the following processes know that no valid user input was returned. If the rS variable is null, then do not run the cR() function.

 

Then, you must allow parameters to be passed into your cR() function. Currently there's no way it could know what x and y are. When you change the parameters to be something like cR(xy) and use the returned object as provided above, you can access the x and y by using xy.x and xy.y .

Known Participant
September 19, 2019
Thanks a lot for the explanations I did as you said but the cancel button still doesn't work If I don't ask too much can you insert your changes directly into the above script so I can understand better?
Disposition_Dev
Legend
September 20, 2019
Can you share what you have after you added what Silly-V suggested? Share the code you're using right now that isn't working (and any error messages) so that we can help you find the bug.