Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

Cancel, stop script

Explorer ,
Sep 19, 2019 Sep 19, 2019

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);

 

 

TOPICS
Scripting
945
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Valorous Hero ,
Sep 19, 2019 Sep 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 .

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 19, 2019 Sep 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?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Sep 20, 2019 Sep 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 23, 2019 Sep 23, 2019

_

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 25, 2019 Sep 25, 2019
LATEST

Thanks for reply, williamadowling

This is my modification...

 

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");

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

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);

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines