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

Need Help with a Conditional when Clicking a Button

Engaged ,
Jun 16, 2022 Jun 16, 2022

Copy link to clipboard

Copied

Hello Everyone,

 

Can anyone Help me with an issue I am having?  

window.JPG

When I select a Radio Button and Click OK  my script works as intended, but if I forget to select a Radio Button and Click OK I need it to Alert me to "Choose a Product Type".   For Var Folder1 I tried to have an if statement that said
IF(choice2 = undefined) {

alert("Choose Product Type");
}
Any suggestions would be appreciated!

var doc = app.activeDocument;

var prefs = load_prefs()                         // try to loads saved prefs
|| {a:true, b:false, c:false, d:false, e:false, choice:"MN"} // or set default prefs

var choice;
var choice2;

var w = new Window ("dialog", "Production Art Save Options");

    var textShow;
  var mySelection = w.add("statictext");
    mySelection.text = "Please Verify Your Location";

var g = w.add("group");
var a = g.add("radiobutton", undefined, "MN");
var b = g.add("radiobutton", undefined, "WA");
var c = g.add("radiobutton", undefined, "CA");
var d = g.add("radiobutton", undefined, "TX");
var e = g.add("radiobutton", undefined, "Remote");

// Product Type
var mySelection1 = w.add("statictext");
mySelection1.text = "Please Select Your Product Type";
var g2 = w.add("group");
var a2 = g2.add("radiobutton", undefined, "Tooled");
var b2 = g2.add("radiobutton", undefined, "Cast");
var c2 = g2.add("radiobutton", undefined, "Etched");
var d2 = g2.add("radiobutton", undefined, "UV");

////////////////////////////////////////////////////////////////////////
// set the radiobutton from the prefs
a.value = prefs.a;
b.value = prefs.b;
c.value = prefs.c;
d.value = prefs.d;
e.value = prefs.e;
choice = prefs.choice; // <-- updated

var radiobuttons = [a, b, c, d, e];
for (var i = 0; i < radiobuttons.length; i++) {
    (function (i) {
        radiobuttons[i].onClick = function () {
            choice = radiobuttons[i].text;
        };
    })(i);
}

var radiobuttons2 = [a2, b2, c2, d2];
for (var x = 0; x < radiobuttons2.length; x++) {
    (function (x) {
        radiobuttons2[x].onClick = function () {
            choice2 = radiobuttons2[x].text;
        };
    })(x); 
}
//////////////////////////////////////////////////////////////////////////

var g3 = w.add("group");
    var okay = g3.add ("button", undefined, "OK"); //Type of button
        okay.onClick = function (){

var sc = "/";	

var folderPrefix;
if (choice == "MN") {
    folderPrefix = ""M:" + sc + "layout" + sc + "layout" + sc;
} else if (choice == "WA") {
    folderPrefix = "W:" + sc + "Art Layouts" + sc;
} else if (choice == "CA") {
    folderPrefix = "D:" + sc + "Public" + sc + "layouts" + sc;
} else if (choice == "TX") {
    folderPrefix = "T:" + sc + "Graphics" + sc + "Layouts" + sc;
} else if (choice == "Remote") {
  folderPrefix = "C:" + sc + "Layout" + sc;
}

var folder1;
if (choice2 = undefined) {
    alert("Please Choose a Product Type");
} else if (choice2 == "Tooled") {
    // Precision Tooled
    folder1 = "Precision Tooled" + sc;
} else if (choice2 == "Cast") {
    // Cast
    folder1 = "Cast" + sc;
} else if (choice2 == "Etched") {
    // Etched
    folder1 = "Etch" + sc;
} else if (choice2 == "UV") {
    // UV Printed
    folder1 = "Giclee-Wayfinding" + sc;
} else if (choice2 == "Ceramic") {
    // Ceramic Insert
    folder1 = "Ceramic_Insert" + sc;
} else if (choice2 == "Bas Relief") {
    // Bas Relief
    folder1 = "Bas Relief" + sc;
}
//alert (folder1);
var strEnter = doc.name; //alert(strEnter);
var soNum = strEnter.slice(0,7);
var strEnter = (soNum); //alert(strEnter);
var folderString = (folderPrefix + folder1); //alert(folderString);
var saveString = (folderString + strEnter + ".ai")
var saveString2 = (folderString + strEnter + ".eps");
var newFile = new File (saveString); //alert(newFile);
var newFile2 = new File (saveString2); //alert(newFile2);

//Create the folder structure if it does not exist
activeFolder = new Folder(folderString);
    if (activeFolder.exists === false) {
    activeFolder.create();
       }

if (choice2 === "Cast"){
    var NamePreset = 'Illustrator 3';
    var saveOpts = new IllustratorSaveOptions()
    saveOpts.viewAfterSaving === false
    
    //Checking for overwrite
	if (newFile.exists === true) {
		if (confirm("Replace existing File?")) {
			doc.saveAs(newFile, saveOpts);
			doc.close;
		}
	} else {
		newFile = new File (saveString);
		doc.saveAs(newFile, saveOpts);
		doc.close;
	}
}else if (choice2 !== "Cast"){
    var NamePreset = 'Illustrator 2020 EPS';
    var saveOpts = new EPSSaveOptions();
    saveOpts.viewAfterSaving === false
    
    //Checking for overwrite
	if (newFile2.exists === true) {
		if (confirm("Replace existing File?")) {
			doc.saveAs(newFile2, saveOpts);
			doc.close;
		}
	} else {
		newFile2 = new File (saveString2);
		doc.saveAs(newFile2, saveOpts);
		doc.close;
	}
}
                w.close();}

    var cancel = g3.add ("button", undefined, "Cancel"); //Type of button
        cancel.onClick = function (){
                alert("You've Cancelled This Operation")
                w.close();}

var myMessage = w.add("statictext");
myMessage.text = "Every Choice You Make Has An End Result   Zig Ziglar";


    w.show ();



TOPICS
Scripting

Views

127

Likes

Translate

Translate

Report

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

correct answers 1 Correct answer

Advisor , Jun 16, 2022 Jun 16, 2022

You're using the assignment operator instead of the equality operator.

if (choice2 = undefined) {
    alert("Please Choose a Product Type");

should be

if (choice2 == undefined) {
    alert("Please Choose a Product Type");

 

Likes

Translate

Translate
Contributor ,
Jun 16, 2022 Jun 16, 2022

Copy link to clipboard

Copied

In some of my scripts, I just disable the OK button at creation and then check for changes in the required radio buttons to actually enable it. I'm on my phone so I don't have any scripts in front of me but it would go something like below...

requiredSetting.onChange = function () {
    // add an if check here to make sure one of your radio buttons have been selected
    // then just enable the OK button
    okButton.enabled = true;
};

 

Likes

Translate

Translate

Report

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
Engaged ,
Jun 16, 2022 Jun 16, 2022

Copy link to clipboard

Copied

I would love to see some script snippets on disabling the OK button!

Likes

Translate

Translate

Report

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
Contributor ,
Jun 16, 2022 Jun 16, 2022

Copy link to clipboard

Copied

This returns (and alerts for testing) the text of whichever radio button is clicked. You just need to make sure to add whichever buttons that can enable the OK button to the `radioButtonsToCheck` array. Also note, since you can't un-click any of the radio buttons after one has been clicked, you don't have to worry about disabling the OK button if you change the product type.

var clickedProductType = showDialog();
if (clickedProductType) alert(clickedProductType);

function showDialog() {
  // window setup
  var win = new Window("dialog");
  win.text = "Forced Selection Example";
  win.orientation = "column";

  // window info
  var infoText = win.add("statictext", undefined, "Please Select Your Product Type");

  // radio buttons
  var gProductTypes = win.add("group");
  gProductTypes.orientation = "row";
  var rbTooled = gProductTypes.add("radiobutton", undefined, "Tooled");
  var rbCast = gProductTypes.add("radiobutton", undefined, "Cast");
  var rbEtched = gProductTypes.add("radiobutton", undefined, "Etched");
  var rbUV = gProductTypes.add("radiobutton", undefined, "UV");

  // window control buttons
  var gButtons = win.add("group");
  gButtons.alignment = "center";
  var buttonOK = gButtons.add("button", undefined, "OK");
  buttonOK.enabled = false;
  var buttonCancel = gButtons.add("button", undefined, "Cancel");

  // enable OK button if a tool is selected
  var radioButtonsToCheck = [rbTooled, rbCast, rbEtched, rbUV];
  var clickedButton = null;
  for (var i = 0; i < radioButtonsToCheck.length; i++) {
    radioButtonsToCheck[i].onClick = function () {
      clickedButton = this.text;
      buttonOK.enabled = true;
    };
  }

  // if "ok" button clicked then return inputs
  if (win.show() == 1) {
    return clickedButton;
  } else {
    return;
  }
}

 

Likes

Translate

Translate

Report

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
Contributor ,
Jun 16, 2022 Jun 16, 2022

Copy link to clipboard

Copied

I forgot that I first mentioned the `onChange` event but that was because I normally work with dropdown menus. You'll need the `onClick` event for radio buttons as I used in my script above.

Likes

Translate

Translate

Report

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
Engaged ,
Jun 16, 2022 Jun 16, 2022

Copy link to clipboard

Copied

LATEST

Thank you!  That is a fantastic bit of code!

Likes

Translate

Translate

Report

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
Advisor ,
Jun 16, 2022 Jun 16, 2022

Copy link to clipboard

Copied

You're using the assignment operator instead of the equality operator.

if (choice2 = undefined) {
    alert("Please Choose a Product Type");

should be

if (choice2 == undefined) {
    alert("Please Choose a Product Type");

 

Likes

Translate

Translate

Report

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
Engaged ,
Jun 16, 2022 Jun 16, 2022

Copy link to clipboard

Copied

I both Love and Hate, it when the answer is that simple!

 

I did have to add the (choice2 == undefined) to the top of my IF/ELSE IF statement (approximately line 126)

if (choice2 == undefined){
    w.close;
}else if (choice2 === "Cast"){

Likes

Translate

Translate

Report

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