stop script on click button close
Copy link to clipboard
Copied
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);
Explore related tutorials & articles
Copy link to clipboard
Copied
At the end of the function that makes the window, have this sort of statement with the .show function:
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
At the end of your function where you have the show(), change it to have a statement like this:
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
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
};

