If/Else Statement Based on Radio Button Selections

Contributor ,
Feb 22, 2022 Feb 22, 2022

Copy link to clipboard

Copied

Any idea why I cannot seem to even get the correct Alert to trigger in this Script?  Thank you for the assist!

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 prodType = load_prodType() ||                                  // try to loads saved prodType
    { a:true, b:false, c:false, d:false, choice1:"Tooled" }      // or set default prodType

    var choice;
    var choice1;


if (app.documents.length > 0) {
    // Creates Panel Window through "win.show();" line of code.
    var win = new Window("dialog","Saving Production Files");
    // Next 2 lines of Code Creates a Group within the Panel
    var panelLocation = win.add("panel", undefined,"Select Your Location");
    var grpLocale = panelLocation.add("group");
    // Creates a Horizontal Row Orientation  for Radio Buttons on Panel Window
    panelLocation.orientation = "row";
    // Creates Radio Buttons on Panel Window
    var rdlMN = panelLocation.add("radiobutton", undefined, "MN");
    var rdlWA = panelLocation.add("radiobutton", undefined, "WA");
    var rdlCA = panelLocation.add("radiobutton", undefined, "CA");
    var rdlTX = panelLocation.add("radiobutton", undefined, "TX");
    var rdlRM = panelLocation.add("radiobutton", undefined, "Remote");

        //Creates Second Group of Radio Buttons on Panel Window
        var panelLocation = win.add("panel", undefined, "Select the Product Type");
        var grpProduction = panelLocation.add("group");
        panelLocation.orientation = "row";
        var rdlTL = panelLocation.add("radiobutton",undefined, "Tooled");
        var rdlCT = panelLocation.add("radiobutton",undefined, "Cast");
        var rdlET = panelLocation.add("radiobutton",undefined, "Etched");
        var rdlUV = panelLocation.add("radiobutton",undefined, "UV Printed");


            //Creates "OK" and "CANCEL" Button Group on Panel Window
            var panelLocation = win.add("panel", undefined, "");
            var grpButton = panelLocation.add("group");
            //Creates a Horizontal Row Orientation  for Buttons on Panel Window
            panelLocation.orientation = "row";

            // Creates OK Button
            // Run the rest of the Script
            btnOK = panelLocation.add("button", undefined, "OK");

            // Creates Cancel Button
            // Close Panel and Does Not run the Script
            btnCancel = panelLocation.add("button", undefined, "Cancel");

// Closes if Statement at beginning of Script.
};

// set the radiobutton from the prefs
rdlMN.value = prefs.a;
rdlWA.value = prefs.b;
rdlCA.value = prefs.c;
rdlTX.value = prefs.d;
rdlRM.value = prefs.e;
choice = prefs.choice; // <-- updated

save_prefs(prefs); // save the prefs

var radiobuttons = [rdlMN, rdlWA, rdlCA, rdlTX, rdlRM];
for (var i = 0; i < radiobuttons.length; i++) {
    (function (i) {
        radiobuttons[i].onClick = function () {
            choice = radiobuttons[i].text;
        };
    })(i);
}



// set the radiobutton from the prodType
rdlTL.value = prodType.a;
rdlCT.value = prodType.b;
rdlET.value = prodType.c;
rdlUV.value = prodType.d;
choice1 = prodType.choice1; // <-- updated

save_prodType(prodType); // save the prodType

var radiobuttons = [rdlTL, rdlCT, rdlET, rdlUV];
for (var i = 0; i < radiobuttons.length; i++) {
    (function (i) {
        radiobuttons[i].onClick = function () {
            choice1 = radiobuttons[i].text;
        };
    })(i);
}




// Instructs Script to Show Panel as detailed above.
win.show();

//-------------------------------
//-------------------------------
//-------------------------------

// functions to load and save perfs

function load_prefs() {
    //    var file = File(Folder.temp + '/perfs.json')
        var file = File('C:/Users/Public/perfs.json')
        return (file.exists) ? $.evalFile(file) : false;
    };
    
    function save_prefs(prefs) {
    //    var file = File(Folder.temp + '/perfs.json')
        var file = File('C:/Users/Public/perfs.json')
        file.open('w');
        file.encoding = 'UTF-8';
        file.write(prefs.toSource());
        file.close();
    };
    
// functions to load and save prodType

function load_prodType() {
//    var file = File(Folder.temp + '/prodType.json')
var file = File('C:/Users/Public/prodType.json')
    return (file.exists) ? $.evalFile(file) : false;
};

function save_prodType(prodType) {
//    var file = File(Folder.temp + '/prodType.json')
var file = File('C:/Users/Public/prodType.json')
    file.open('w');
    file.encoding = 'UTF-8';
    file.write(prodType.toSource());
    file.close();
};

//-------------------------------
//-------------------------------
//-------------------------------
var info = "Make a Selection Please";
if ((rdlMN === true) && (rdlTL === true)) { info ="Mn Tooled";}
    else if ((rdlMN === true) && (rdlCT === true)) { info = "Mn Cast";}
    else if ((rdlMN === true) && (rdlET === true)) { info = "Mn Etched";}
    else if ((rdlMN === true) && (rdlUV === true)) { info = "Mn UV";}    

alert(info);
TOPICS
Scripting

Views

121

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 ,
Feb 22, 2022 Feb 22, 2022

Copy link to clipboard

Copied

I am able to get the "MN Tooled" alert to show now, but not the other ones.  Here's what I changed in my IF/ELSE statement

var info = "Make a Selection Please";
if ((prefs.a === true) && (rdlTL === true)) { info ="Mn Tooled";}
    else if ((prefs.a === true) && (prodType.b === true)) { info = "Mn Cast";}
    else if ((prefs.a === true) && (prodType. === true)) { info = "Mn Etched";}
    else if ((prefs.a === true) && (prodType.d === true)) { info = "Mn UV";}    

alert(info);

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
Adobe Community Professional ,
Feb 22, 2022 Feb 22, 2022

Copy link to clipboard

Copied

There is a typo here:

prodType. === 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
Contributor ,
Feb 22, 2022 Feb 22, 2022

Copy link to clipboard

Copied

odd some how that ended up in my post but not my script...

 

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 ,
Feb 22, 2022 Feb 22, 2022

Copy link to clipboard

Copied

 So the Default options are selected based on the "prefs.JSON"

({a:true, b:false, c:false, d:false, e:false, choice:"MN"})

 

and the "prodType.JSON"

({a:false, b:true, c:false, d:false, choice1:"Cast"})

 

but the Save Functions to update those 2 JSON files does not seem to be overwritting the files when the selection changes.

 

 

 

    function save_prefs(prefs) {
    //    var file = File(Folder.temp + '/perfs.json')
        var file = File('C:/Users/Public/perfs.json')
        file.open('w');
        file.encoding = 'UTF-8';
        file.write(prefs.toSource());
        file.close();
    };
    



function save_prodType(prodType) {
//    var file = File(Folder.temp + '/prodType.json')
var file = File('C:/Users/Public/prodType.json')
    file.open('w');
    file.encoding = 'UTF-8';
    file.write(prodType.toSource());
    file.close();
};

 

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 ,
Feb 22, 2022 Feb 22, 2022

Copy link to clipboard

Copied

The issue seems to be wth the fact that my prodType.JSON file is not being Saved/Updated based on my selection.

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
Adobe Community Professional ,
Feb 22, 2022 Feb 22, 2022

Copy link to clipboard

Copied

Alright, I think I know why this is happening now, it's probably because there needs to be an event code which calls your save_prefs function and the like. Right now, it appears that you have set up your UI, while doing so, write down your unchanged data to the prefs files and then wait for user action which at that point it's already done with your intentions before the user is presented with the window dialog.

So there are a couple of ways to save these files: 1) save it each time the choice or choice1 are changed within the onClick events, 2) save it only at the end of when the user closes the dialog.
In the show invocation:
if (w.show() == 2) {
  // maybe ignore the settings if the user has cancelled.
  return null;
} else {
  save_prefs(...)
  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 ,
Feb 23, 2022 Feb 23, 2022

Copy link to clipboard

Copied

@Silly-V  I couldn't get either option to work for me 😞

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 ,
Feb 23, 2022 Feb 23, 2022

Copy link to clipboard

Copied

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 prodType = load_prodType() ||                                  // try to loads saved prodType
    { a:true, b:false, c:false, d:false, choice1:"Tooled" }      // or set default prodType

    var choice;
    var choice1;

if (app.documents.length > 0) {
    // Creates Panel Window through "win.show();" line of code.
    var win = new Window("dialog","Saving Production Files");
    // Next 2 lines of Code Creates a Group within the Panel
    var panelLocation = win.add("panel", undefined,"Select Your Location");
    var grpLocale = panelLocation.add("group");
    // Creates a Horizontal Row Orientation  for Radio Buttons on Panel Window
    panelLocation.orientation = "row";
    // Creates Radio Buttons on Panel Window
    var rdlMN = panelLocation.add("radiobutton", undefined, "MN");
    var rdlWA = panelLocation.add("radiobutton", undefined, "WA");
    var rdlCA = panelLocation.add("radiobutton", undefined, "CA");
    var rdlTX = panelLocation.add("radiobutton", undefined, "TX");
    var rdlRM = panelLocation.add("radiobutton", undefined, "Remote");

        //Creates Second Group of Radio Buttons on Panel Window
        var panelLocation = win.add("panel", undefined, "Select the Product Type");
        var grpProduction = panelLocation.add("group");
        panelLocation.orientation = "row";
        var rdlTL = panelLocation.add("radiobutton",undefined, "Tooled");
        var rdlCT = panelLocation.add("radiobutton",undefined, "Cast");
        var rdlET = panelLocation.add("radiobutton",undefined, "Etched");
        var rdlUV = panelLocation.add("radiobutton",undefined, "UV Printed");

            //Creates "OK" and "CANCEL" Button Group on Panel Window
            var panelLocation = win.add("panel", undefined, "");
            var grpButton = panelLocation.add("group");
            //Creates a Horizontal Row Orientation  for Buttons on Panel Window
            panelLocation.orientation = "row";

            // Creates OK Button
            // Run the rest of the Script
            btnOK = panelLocation.add("button", undefined, "OK");

            // Creates Cancel Button
            // Close Panel and Does Not run the Script
            btnCancel = panelLocation.add("button", undefined, "Cancel");

// Closes if Statement at beginning of Script.
};
// set the radiobutton from the prefs
rdlMN.value = prefs.a;
rdlWA.value = prefs.b;
rdlCA.value = prefs.c;
rdlTX.value = prefs.d;
rdlRM.value = prefs.e;
choice = prefs.choice; // <-- updated

save_prefs(prefs); // save the prefs

var radiobuttons = [rdlMN, rdlWA, rdlCA, rdlTX, rdlRM];
for (var i = 0; i < radiobuttons.length; i++) {
    (function (i) {
        radiobuttons[i].onClick = function () {
            choice = radiobuttons[i].text;
        };
    })(i);
}

// set the radiobutton from the prodType
rdlTL.value = prodType.a;
rdlCT.value = prodType.b;
rdlET.value = prodType.c;
rdlUV.value = prodType.d;
choice1 = prodType.choice1; // <-- updated

save_prodType(prodType); // save the prodType

var radiobuttons1 = [rdlTL, rdlCT, rdlET, rdlUV];
for (var i = 0; i < radiobuttons1.length; i++) {
    (function (i) {
        radiobuttons1[i].onClick = function () {
            choice1 = radiobuttons1[i].text;
        };
    })(i);
}

// Instructs Script to Show Panel as detailed above.
//win.show();

if (win.show() == 2) {
    alert ("Save Cancelled");
  } else {
    save_prefs(prefs);
    save_prodType(prodType);
    alert ("Save Complete");
  }

//-------------------------------
//-------------------------------
//-------------------------------

// functions to load and save perfs

function save_prefs(prefs) {
    //    var file = File(Folder.temp + '/perfs.json')
        var file = File('C:/Users/Public/perfs.json')
        file.open('w');
        file.encoding = 'UTF-8';
        file.write(prefs.toSource());
        file.close();
    };
    function save_prodType(prodType) {
        //    var file = File(Folder.temp + '/prodType.json')
        var file = File('C:/Users/Public/prodType.json')
            file.open('w');
            file.encoding = 'UTF-8';
            file.write(prodType.toSource());
            file.close();
        };

function load_prefs() {
    //    var file = File(Folder.temp + '/perfs.json')
        var file = File('C:/Users/Public/perfs.json')
        return (file.exists) ? $.evalFile(file) : false;
    };
        
// functions to load and save prodType

function load_prodType() {
//    var file = File(Folder.temp + '/prodType.json')
var file = File('C:/Users/Public/prodType.json')
    return (file.exists) ? $.evalFile(file) : false;
};

//-------------------------------
//-------------------------------
//-------------------------------
var info = "You Shall Not Pass!";
if ((prefs.a === true) && (prodType.a === true)) { info = "MN Tooled";}
    else if ((prefs.a === true) && (prodType.b === true)) { info = "MN Cast";}
    else if ((prefs.a === true) && (prodType.c === true)) { info = "MN Etched";}
    else if ((prefs.a === true) && (prodType.d === true)) { info = "MN UV Printed";}
    else if ((prefs.b === true) && (prodType.a === true)) { info = "WA Tooled";}
    else if ((prefs.b === true) && (prodType.b === true)) { info = "WA Cast";}
    else if ((prefs.b === true) && (prodType.c === true)) { info = "WA Etched";}
    else if ((prefs.b === true) && (prodType.d === true)) { info = "WA UV Printed";}
    else if ((prefs.c === true) && (prodType.a === true)) { info = "CA Tooled";}
    else if ((prefs.c === true) && (prodType.b === true)) { info = "CA Cast";}
    else if ((prefs.c === true) && (prodType.c === true)) { info = "CA Etched";}
    else if ((prefs.c === true) && (prodType.d === true)) { info = "CA UV Printed";}
    else if ((prefs.d === true) && (prodType.a === true)) { info = "TX Tooled";}
    else if ((prefs.d === true) && (prodType.b === true)) { info = "TX Cast";}
    else if ((prefs.d === true) && (prodType.c === true)) { info = "TX Etched";}
    else if ((prefs.d === true) && (prodType.d === true)) { info = "TX UV Printed";}
    else if ((prefs.e === true) && (prodType.a === true)) { info = "Remote Tooled";}
    else if ((prefs.e === true) && (prodType.b === true)) { info = "Remote Cast";}
    else if ((prefs.e === true) && (prodType.c === true)) { info = "Remote Etched";}
    else if ((prefs.e === true) && (prodType.d === true)) { info = "Remote UV Printed";}    

alert(info);

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
Adobe Community Professional ,
Feb 23, 2022 Feb 23, 2022

Copy link to clipboard

Copied

LATEST

The question is, is the variable 'prodType' and others which you are wishing to update, are they actually being updated in any way?
var prodType = { choice1: "A" };
var choice1 = prodType.choice1;
choice1 = "B";

alert(choice1); // "B"

alert(prodType.choice1); // "A"

As you see, 'choice1' has not been coupled by reference to the property inside the object of 'prodType'. Updating the 'choice1' independent variable does not actually mutate the property inside prodType because it is a primitive property, as it is only a string. To mutate properties by reference it's only possible to have this property be an object itself, and you change something inside of that object.

var prodType = { choice1: { value : "A" } };
var choice1 = prodType.choice1;
choice1.value = "B";

alert(choice1.value); // "B"

alert(prodType.choice1.value); // "B"

 

The simplest short answer is though, when you are changing the value of 'choice1', just set the text of the button to the prodType's '.choice1' property instead of the choice1 variable.

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