Hey supergeniuses! I'm somewhat new to scripting in InDesign, although I did some Illustrator scripting a couple years ago. I'm familiar enough with javascript to get things done, but I'm not 100% fluent.
I'm trying to get back into it, and I wrote this script that takes a selected collection and scales them all down to the same width or height (including stroke widths in the width/height) based on the object in the collection with the smallest or largest dimension, or some manually entered dimensions. There's an option also to constrain proportions when scaling.
The script itself works pretty well actually! My problem though, is that when I hit cancel in the dialog window, rather than exiting the script, it runs into the validation and gives me a "Choose Options" alert. Here is the code for the dialog window and the validation lines (yeah I know I'm kinda unnecessarily redundant and messy, but it helps me keep things straight):
var dlg=new Window('dialog', 'Scale Multiple Objects');
dlg.alignChildren="fill";
dlg.options2=dlg.add('panel', undefined, 'Dimension', {borderStyle:'sunken'});
dlg.options=dlg.add('panel', undefined, 'Object', {borderStyle:'sunken'});
dlg.options.add('statictext', undefined, 'Choose which object will determine the chosen dimension.');
dlg.options.optionbtns=dlg.options.add('group');
dlg.options.optionbtns.orientation="column";
dlg.options.optionbtns.optionSmaller=dlg.options.optionbtns.add('radiobutton', undefined, 'Object with smallest dimension.');
dlg.options.optionbtns.optionBigger=dlg.options.optionbtns.add('radiobutton', undefined, 'Object with largest dimension.');
dlg.options2.add('statictext', undefined, 'Choose which dimension will be scaled.');
dlg.options2.optionbtns2=dlg.options2.add('group');
dlg.options2.optionbtns2.orientation="row";
dlg.options2.optionbtns2.optionWidth=dlg.options2.optionbtns2.add('radiobutton', undefined, 'Width');
dlg.options2.optionbtns2.optionHeight=dlg.options2.optionbtns2.add('radiobutton', undefined, 'Height');
dlg.add('panel', undefined, undefined);
dlg.or=dlg.add('panel', undefined, undefined, {borderStyle:'raised'});
dlg.or.alignChildren="center";
dlg.or.add('statictext', undefined, 'OR');
dlg.add('panel', undefined, undefined);
dlg.manual=dlg.add('panel', undefined, 'Scale to Manual Dimensions', {borderStyle:'sunken'});
dlg.manual.add('statictext', undefined, 'Alternatively, enter manual dimensions to scale the items to.');
dlg.manual.add('statictext', undefined, 'You may enter either one or both dimensions.');
dlg.manual.textboxes1=dlg.manual.add('group');
dlg.manual.textboxes1.orientation="row";
dlg.manual.textboxes1.add('statictext', undefined, 'Width:');
dlg.manual.textboxes1.manualWidth=dlg.manual.textboxes1.add('edittext', undefined, undefined);
dlg.manual.textboxes1.manualWidth.characters=10;
dlg.manual.textboxes1.add('statictext', undefined, app.activeDocument.viewPreferences.horizontalMeasurementUnits.toString().toLowerCase());
dlg.manual.textboxes2=dlg.manual.add('group');
dlg.manual.textboxes2.orientation="row";
dlg.manual.textboxes2.add('statictext', undefined, 'Height:');
dlg.manual.textboxes2.manualHeight=dlg.manual.textboxes2.add('edittext', undefined, undefined);
dlg.manual.textboxes2.manualHeight.characters=10;
dlg.manual.textboxes2.add('statictext', undefined, app.activeDocument.viewPreferences.verticalMeasurementUnits.toString().toLowerCase());
dlg.aspectRatio=dlg.add('checkbox', undefined, 'Scale items proportionally?');
dlg.btns=dlg.add("group");
dlg.btns.orientation="row";
dlg.btns.cancel=dlg.btns.add("button", undefined, "Cancel", {name:"cancel"});
dlg.btns.add("button", undefined, "OK", {name:"ok"});
dlg.show();
if((dlg.manual.textboxes1.manualWidth.text==""&&dlg.manual.textboxes2.manualHeight.text=="")&&((dlg.options2.optionbtns2.optionWidth.value==false&&dlg.options2.optionbtns2.optionHeight.value==false)||(dlg.options.optionbtns.optionSmaller.value==false&&dlg.options.optionbtns.optionBigger.value==false))){
alert("Choose options.");
exit();
}
if((dlg.manual.textboxes1.manualWidth.text!=""||dlg.manual.textboxes2.manualHeight.text!="")&&((dlg.options2.optionbtns2.optionWidth.value!=false||dlg.options2.optionbtns2.optionHeight.value!=false)||(dlg.options.optionbtns.optionSmaller.value!=false||dlg.options.optionbtns.optionBigger.value!=false))){
alert("Choose only one mode.");
}
So why is it alerting me with "Choose options." when I hit cancel? It's probably something basic and dumb but I just can't seem to figure it out. I'm the first to admit I have not much idea how OK/Cancel even works...so any help would be amazing.
> It's probably something basic and dumb but I just can't seem to figure it out.
Your call to dlg.show() actually returns the value of the button that was selected. You simply ignore it, that's all.
See Peter K.'s "ScriptUI for Dummies" ((ducks) really, that's the title!), the Guide to Life, the Universe, and Everything Else, as far as ScriptUI is concerned: http://www.kahrel.plus.com/indesign/scriptui.html
When the script hits the show() line, it shows the dialog and waits for
it to be dismissed (or if you've set up any event listeners, obviously
it deals with them). Once the window is dismissed (whatever value the
show() returns [and show() does return a value which indicates HOW the
dialog was dismissed]) the script simply continues to the next line and
executes that. That happens to be a line that gives an alert(show
options) box in your case.
So to avoid that:
Do a check to see what value show() returns. I can't remember offhand
the value for cancel. Maybe 2. So if dlg.show() == 2 just do an exit().
Or conversly, if dlg.show() != 2 do the rest of the stuff.
But you'll need to experiment a little do see what the difference value
for show() are. Just do alert(dlg.show()) and try exiting with different
buttons.
HTH,
Ariel
Daer Amperjess,
Please use the below source code, it will solve your problem:
//======================= Dialog Box - Start ============================//
var dlg=new Window('dialog', 'Scale Multiple Objects');
dlg.alignChildren="fill";
dlg.options2=dlg.add('panel', undefined, 'Dimension', {borderStyle:'sunken'});
dlg.options=dlg.add('panel', undefined, 'Object', {borderStyle:'sunken'});
dlg.options.add('statictext', undefined, 'Choose which object will determine the chosen dimension.');
dlg.options.optionbtns=dlg.options.add('group');
dlg.options.optionbtns.orientation="column";
dlg.options.optionbtns.optionSmaller=dlg.options.optionbtns.add('radi obutton', undefined, 'Object with smallest dimension.');
dlg.options.optionbtns.optionBigger=dlg.options.optionbtns.add('radio button', undefined, 'Object with largest dimension.');
dlg.options2.add('statictext', undefined, 'Choose which dimension will be scaled.');
dlg.options2.optionbtns2=dlg.options2.add('group');
dlg.options2.optionbtns2.orientation="row";
dlg.options2.optionbtns2.optionWidth=dlg.options2.optionbtns2.add('ra diobutton', undefined, 'Width');
dlg.options2.optionbtns2.optionHeight=dlg.options2.optionbtns2.add('r adiobutton', undefined, 'Height');
dlg.add('panel', undefined, undefined);
dlg.or=dlg.add('panel', undefined, undefined, {borderStyle:'raised'});
dlg.or.alignChildren="center";
dlg.or.add('statictext', undefined, 'OR');
dlg.add('panel', undefined, undefined);
dlg.manual=dlg.add('panel', undefined, 'Scale to Manual Dimensions', {borderStyle:'sunken'});
dlg.manual.add('statictext', undefined, 'Alternatively, enter manual dimensions to scale the items to.');
dlg.manual.add('statictext', undefined, 'You may enter either one or both dimensions.');
dlg.manual.textboxes1=dlg.manual.add('group');
dlg.manual.textboxes1.orientation="row";
dlg.manual.textboxes1.add('statictext', undefined, 'Width:');
dlg.manual.textboxes1.manualWidth=dlg.manual.textboxes1.add('edittext ', undefined, undefined);
dlg.manual.textboxes1.manualWidth.characters=10;
dlg.manual.textboxes1.add('statictext', undefined, app.activeDocument.viewPreferences.horizontalMeasurementUnits.toStrin g().toLowerCase());
dlg.manual.textboxes2=dlg.manual.add('group');
dlg.manual.textboxes2.orientation="row";
dlg.manual.textboxes2.add('statictext', undefined, 'Height:');
dlg.manual.textboxes2.manualHeight=dlg.manual.textboxes2.add('edittex t', undefined, undefined);
dlg.manual.textboxes2.manualHeight.characters=10;
dlg.manual.textboxes2.add('statictext', undefined, app.activeDocument.viewPreferences.verticalMeasurementUnits.toString( ).toLowerCase());
dlg.aspectRatio=dlg.add('checkbox', undefined, 'Scale items proportionally?');
dlg.btns=dlg.add("group");
dlg.btns.orientation="row";
dlg.btns.cancel=dlg.btns.add("button", undefined, "Cancel", {name:"cancel"});
dlg.btns.ok = dlg.btns.add("button", undefined, "OK", {name:"ok"});
dlg.btns.ok.onClick= function()
{
if((dlg.manual.textboxes1.manualWidth.text==""&&dlg.manual.textboxes2 .manualHeight.text=="")&&((dlg.options2.optionbtns2.optionWidth.value= =false&&dlg.options2.optionbtns2.optionHeight.value==false)||(dlg.opti ons.optionbtns.optionSmaller.value==false&&dlg.options.optionbtns.opti onBigger.value==false))){
alert("Choose options.");
exit();
}
if((dlg.manual.textboxes1.manualWidth.text!=""||dlg.manual.textboxes2 .manualHeight.text!="")&&((dlg.options2.optionbtns2.optionWidth.value! =false||dlg.options2.optionbtns2.optionHeight.value!=false)||(dlg.opti ons.optionbtns.optionSmaller.value!=false||dlg.options.optionbtns.opti onBigger.value!=false))){
alert("Choose only one mode.");
}
}
dlg.btns.cancel.onClick = function()
{
dlg.close();
exit(0);
};
dlg.show();
//====================== Dialog Box --- End ==========================//
Please let me know if you have any queries.
Thanks & Regards
T.R.Harihara SudhaN
North America
Europe, Middle East and Africa
Asia Pacific