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

Radio Button Selection needs to be remembered

Engaged ,
Oct 21, 2024 Oct 21, 2024

Copy link to clipboard

Copied

I have an array variable called "products".  Each product has a radio button generated for it.  Is there a way to have the script remember the radio button selection made the previous time the script was used?

Thanks!

TOPICS
How-to , Performance , Scripting

Views

82

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
Engaged ,
Oct 21, 2024 Oct 21, 2024

Copy link to clipboard

Copied

    var userPath = $.getenv("USERPROFILE");
    var cloudPath = decodeURI("/Layouts - Documents/");
    var savePath = userPath + cloudPath;
    // setup each possible location and it's save path
    var locations = {
      Sharepoint: savePath,
      Local: "C:/Layout/",
    };
    // setup each possible product type and it's save path folder name
    var products = {
      Tool: "Tooled/",
      Molten: "Molten/",
      Buff: "Buff/",
      HPPRINT: "Print/",
    };
//****************************************************************************** */

function settingsWin(location, product) {
      // settings window
      var win = new Window("dialog");
      win.text = "Production Art Save Options";
  
      // create a radio button for each location
      win.add("statictext", undefined, "Please Verify Your Location");
      var gLocation = win.add("group");
      var rb;
      var locationRBs = [];
      for (var k in locations) {
        var rb = gLocation.add("radiobutton", undefined, k);
        if (location == k) rb.value = true;
        locationRBs.push(rb);

    // Automatically select Sharepoint by default
    if (k === "Sharepoint" || location == k) rb.value = true;
    
    locationRBs.push(rb);
      }
  
      // create a radio button for each product
      win.add("statictext", undefined, "Select Your Product Type");
      var gProducts = win.add("group");
      var rb;
      var productRBs = [];
      for (var k in products) {
        var rb = gProducts.add("radiobutton", undefined, k);
        if (product == k) rb.value = true;
        productRBs.push(rb);
      }
  
      //--------------------------------------------------------------------------------------------------------
  
      // pattern required panel
      var gPatternRequired = win.add("group");
      gPatternRequired.orientation = "column"; // set the orientation to column
      gPatternRequired.alignChildren = ["center", "top"]; // center align the children vertically
  
      var patternText = gPatternRequired.add(
        "statictext",
        undefined,
        "Pattern Required?"
      );
      patternText.alignment = "center"; // center align the static text
  
      var patternRBGroup = gPatternRequired.add("group");
      patternRBGroup.alignment = "center"; // center align the radio button group
  
      var patternYesRB = patternRBGroup.add("radiobutton", undefined, "Yes");
      var patternNoRB = patternRBGroup.add("radiobutton", undefined, "No");
      patternNoRB.value = true;
  
      //---------------------------------------------------------------------------------------------------------
  
      // group - window buttons
      var gWindowButtons = win.add("group", undefined);
      gWindowButtons.orientation = "row";
      gWindowButtons.alignChildren = ["Leftwards", "center"];
      gWindowButtons.alignment = ["center", "top"];
      var btOK = gWindowButtons.add("button", undefined, "OK");
      var btCancel = gWindowButtons.add("button", undefined, "Cancel");
      var btDefaults = gWindowButtons.add("button", undefined, "Set Defaults");
  
      // quote panel
      var pInfo = win.add("panel", undefined);
      var quote = "Every Choice You Make Has An End Result";
      var author = "Zig Ziglar";
      pInfo.add("statictext", undefined, quote);
      pInfo.add("statictext", undefined, "- " + author);
  
      // save defaults
      var selectedProduct;
      btDefaults.onClick = function () {
        selectedProduct = captureRBSelection(productRBs);
        if (selectedProduct) {
          writeJSONData({ product: selectedProduct }, defaultProduct);
          //alert("Default Product saved as " + selectedProduct + ".");
        } else {
          alert("Make sure to select both a Location and a Product first!");
        }
      };
  
      function captureRBSelection(rbs) {
        // check to see which product was selected
        var selection = null;
        for (var i = 0; i < rbs.length; i++) {
          if (rbs[i].value) selection = rbs[i].text;
        }
        return selection;
      }
  
    // if "ok" button clicked then return inputs
    if (win.show() == 1) {
        var selectedLocation;
        selectedLocation = captureRBSelection(locationRBs);
        selectedProduct = captureRBSelection(productRBs);
  
        // make sure both a location and a product was selected before continuing
        if (selectedLocation && selectedProduct) {
          return {
            location: selectedLocation,
            product: selectedProduct,
            pattern: patternYesRB.value,
          };
        } else {
          alert("Make sure to select both a Location and a Product first!");
          return;
        }
      } else {
        return;
      }
    }

Votes

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
Community Expert ,
Oct 21, 2024 Oct 21, 2024

Copy link to clipboard

Copied

LATEST

Hi @BryanPagenkopf, here are two methods:

(function () {

    var key = "myPrefKey";

    /* --------------------------- *
     * METHOD 1: ENVIRONMENT VAR   *
     * --------------------------- */

    // read environment var
    var myEnvironmentValue = $.getenv(key)

    $.writeln('myEnvironmentValue = ' + myEnvironmentValue);

    // set environment var
    $.setenv(key, "The quick brown fox");


    /* --------------------------- *
     * METHOD 2: APP PREFERENCE    *
     * --------------------------- */

    // read preference record
    var myPreferenceValue = app.preferences.getStringPreference(key);
    $.writeln('myPreferenceValue = ' + myPreferenceValue);

    // set preference record
    app.preferences.setStringPreference(key, 'jumps over the lazy dog');

})();

On the first run both will be null/empty and on subsequent runs they will be show the value.

which returns "myPref = null" on the first run, and "myPref = The quick brown fox" on subsequent runs.

 

Environment vars are only present for the session, so once you quit Illustrator they will disappear. Preference values persist across sessions because they are stored in Illustrator's preference file..

 

And of course you can write your own preference file as a text file. This is actually a good way to go for scripts with complex settings.

- Mark

Votes

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