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

stop script on click button close

Explorer ,
Jan 26, 2021 Jan 26, 2021

this example script creates a rectangle ...
the problem is that when you press cancel or x button the script continues to run ..
how to make to stop functions when i press cancel or exit ?

 

 

var doc = app.activeDocument;


function sPanel() {  
    var unit;  
    var units = ['mm','pt','cm'];

    var rectanglePanel = new Window("dialog", "", undefined, { borderless: false });
    rectanglePanel.alignChildren = "left";

    var rand0 = rectanglePanel.add ("group");
    rand0.add ('statictext {text: "Units", characters: 8, justify: "left"}');
    var dims = rand0.add("dropdownlist", undefined, units);
    dims.selection = dims.items[0];

    var rand1 = rectanglePanel.add ("group");
    rand1.add ('statictext {text: "width", characters: 8, justify: "left"}');
    var lat = rand1.add("edittext",undefined,"148");
    lat.characters = 10;
   
    var rand2 = rectanglePanel.add ("group");
    rand2.add ('statictext {text: "height", characters: 8, justify: "left"}');
    var ina = rand2.add("edittext",undefined,"210");
    ina.characters = 10;

	var rand6 = rectanglePanel.add ("group");
    rand6.add("button", undefined, "OK");
    rand6.add("button", undefined, "Cancel");
    
    rectanglePanel.show(); 
    
    unit = dims.selection.text;  
    x = Number(new UnitValue(lat.text + " " + unit).as('pt'));
    i =  Number(new UnitValue(ina.text + " " + unit).as('pt'));
    
    return [x,i];
 };


function cRectangle() {  

     
    var layer = doc.layers[0];
    layer.name = "guides"; 
    doc.rulerOrigin = [0,0]; 
     
                
                
                rectangle = layer.pathItems.rectangle(x, i, i, x,false); 
                rectangle.fillColor = NoColor; rectangle.stroked = false; rectangle.guides = true;
                
return rectangle;
}  
var rectangleSize = sPanel();  
cRectangle(rectangleSize);  






 

 

TOPICS
Scripting
627
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 ,
Jan 26, 2021 Jan 26, 2021

At the end of the function that makes the window, have this sort of statement with the .show function:

 

  if (w.show() == 2) {
    return null;
  } else {
    var res = {}; // whatever data you are wanting from the dialog.
    return res;
  }
 
Then when you call the dialog you can launch your process after it is dismissed.
var myInput = makeMyDialog();
if (myInput == null) {
  alert("Cancelled");
} else {
  doMyProcess(myInput);
}
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 ,
Jan 28, 2021 Jan 28, 2021

thanks for answer but I can't figure out how to do thatt..i'm quite a beginner

to be better understood  I made another simple script panel 

when you click ok =  a text is created

press cancel  = the window panel closes but  nothing is happening, the text still appears in illustrator....

 

    var doc = app.activeDocument;

// panel_____________   
   
    var sPanel = new Window("dialog", "mypanel", undefined, { borderless: false });
    sPanel.alignChildren = "left";
    var r1 = sPanel.add ("group"); r1.add ('statictext {text: "text", characters: 8, justify: "left"}');
    var r2 = r1.add("edittext",undefined,"example"); r2.characters = 10; r2.active = true;
    var r3 = sPanel.add ("group"); 
    r3.add("button", undefined, "OK"); 
    r3.add("button", undefined, "Cancel"); 
    sPanel.show(); 

 // text_____________   

var myTextFrame = doc.textFrames.add();
myTextFrame.contents = r2.text + " ";
var fontStyle = myTextFrame.textRange.characterAttributes; 
fontStyle.textFont = app.textFonts.getByName("Verdana"); fontStyle.size = 200;

 

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
Valorous Hero ,
Jan 28, 2021 Jan 28, 2021

At the end of your function where you have the show(), change it to have a statement like this:

 if (w.show() == 2) {
    return null;
  } else {
    var res = {}; // whatever data you are wanting from the dialog.
    return res;
  }
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
Guide ,
Jan 28, 2021 Jan 28, 2021

 

var doc = app.activeDocument;

// panel_____________   
   
var sPanel = new Window("dialog", "mypanel", undefined, { borderless: false });
sPanel.alignChildren = "left";
var r1 = sPanel.add ("group"); r1.add ('statictext {text: "text", characters: 8, justify: "left"}');
var r2 = r1.add("edittext",undefined,"example"); r2.characters = 10; r2.active = true;
var r3 = sPanel.add ("group"); 
r3.add("button", undefined, "OK"); 
r3.add("button", undefined, "Cancel"); 

if (sPanel.show() == 1 ) {  // if you clicked OK
      
    // text_____________   

    var myTextFrame = doc.textFrames.add();
    myTextFrame.contents = r2.text + " ";
    var fontStyle = myTextFrame.textRange.characterAttributes; 
    fontStyle.textFont = app.textFonts.getByName("Verdana"); fontStyle.size = 200;

}

 

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
Participant ,
Jan 29, 2021 Jan 29, 2021
LATEST

Would you not just add a click listener to the OK button?

 

As well as making your script more readable, if they close the script or exit the window nothing happens. For example...

 

var button = parent.add("button", undefined, 'Ok');
button.onClick = function() {
   //Do something when the ok button is pressed
};

 

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