9 Replies Latest reply on Sep 5, 2016 2:35 AM by Obi-wan Kenobi

    Script-UI and "Reset button"

    Obi-wan Kenobi Adobe Community Professional

      Hello, it's (always) me!  [ as sings Adele!  ]

       

      I would like to apply a condition to paras choosing them by their para style through an UI!

       

      Capture d’écran 2016-09-03 à 13.58.34.png

       

      When I launch the script:

       

      Capture d’écran 2016-09-03 à 13.58.53.png

       

      … the script creates (if no exists) the condition "Jump".

       

      Then, through the UI, I can choose my targets! Here: "Title" and "Sub-Title".

       

      Capture d’écran 2016-09-03 à 13.59.17.png

       

      I click on "OK". The script applies the condition "Jump" to the paras with "Title" and "Sub-Title" styles!

       

      Capture d’écran 2016-09-03 à 13.59.41.png

       

      The problems come here:

      When I launch the script again, "Title" and "Sub-Title" styles aren't selected!

       

      First question: is there a simple way to "save" the previous settings?

       

      I launch the script:

       

      Capture d’écran 2016-09-03 à 14.01.12.png

       

      I want to "reset"! I mean remove the applied "Jump" condition in all the doc.

       

      The code I've associated to the button is:

       

                  var buttons = w.add ('group {alignment: "center"}');
                        ResetButton = buttons.add ('button {text: "Reset"}');
                        buttons.add ('button {text: "OK"}');
                        buttons.add ('button {text: "Cancel"}');
      
      
                  var group11 = w.add ('group');
                        group11.add ('statictext {text: "by Michel Allio, september 2016. At your own risks! (^/)"}');
                        
      //////////////////////////////////////////////////////////////////////                  
      
      
      // Reset
      ResetButton.onClick = function () {  
      myCondition.remove();
      myCondition = app.activeDocument.conditions.add ({name: "Jump", indicatorColor: UIColors.WHITE, indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
      }  
      
      

       

      … But no effect!

       

      Thanks for your help!

       

      (^/)

        • 1. Re: Script-UI and "Reset button"
          Loic.Aigon Adobe Community Professional

          Hi

          if you are using a modal mode Window, your dialogue won't let you interact witz indesign untel it's closed.

          Hth

          Loic

          • 2. Re: Script-UI and "Reset button"
            Obi-wan Kenobi Adobe Community Professional

            Hi Loïc,

             

            Yeap! … but is there a turn-around? 

             

            (^/)

            • 3. Re: Script-UI and "Reset button"
              Kasyan Servetsky Level 5

              I'll try to answer question #1.

              I use insertLabel and ectractLabel methods to save-restore dialog settings. Here's a very simple script which I use as a template for new scripts:

              var scriptName = "Script name",
              doc, set;
              
              PreCheck();
              //===================================== FUNCTIONS ======================================
              function Main() {
                  var count = 0;
                  
                  
                  
                  var report = count + " item" + ((count == 1) ? " was" : "s were") + " processed.";
                  alert("Finished. " + report, scriptName);
              }
              //--------------------------------------------------------------------------------------------------------------------------------------------------------
              function CreateDialog() {
                  GetDialogSettings();
                  var w = new Window("dialog", scriptName);
                  
                  w.p = w.add("panel", undefined, "Panel:");
                  w.p.orientation = "column";
                  w.p.alignment = "fill";
                  w.p.alignChildren = "left";
                  
                  w.p.g = w.p.add("group");
                  w.p.g.orientation = "row";
                  w.p.g.st = w.p.g.add("statictext", undefined, "Static text:");
                  w.p.g.et = w.p.g.add("edittext", undefined, set.editText);
                  w.p.g.et.characters = 20;
              
                  // Buttons
                  w.bts = w.add("group");
                  w.bts.orientation = "row";   
                  w.bts.alignment = "center";
                  w.bts.ok = w.bts.add("button", undefined, "OK", {name:"ok"});
                  w.bts.cancel = w.bts.add("button", undefined, "Cancel", {name:"cancel"});
                  
                  var showDialog = w.show();
                  
                  if (showDialog == 1) {
                      set.editText = w.p.g.et.text;
                      
                      app.insertLabel("Kas_" + scriptName, set.toSource());
                      Main();
                  }    
              }    
              //--------------------------------------------------------------------------------------------------------------------------------------------------------
              function GetDialogSettings() {
                  set = eval(app.extractLabel("Kas_" + scriptName));
                  if (set == undefined) {
                      set = {editText: "Edit text"};
                  }
              
                  return set;
              }    
              //--------------------------------------------------------------------------------------------------------------------------------------------------------
              function PreCheck() {
                  if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);
                  doc = app.activeDocument;
                  if (doc.converted) ErrorExit("The current document has been modified by being converted from older version of InDesign. Please save the document and try again.", true);
                  if (!doc.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);
                  CreateDialog();
              }
              //--------------------------------------------------------------------------------------------------------------------------------------------------------
              function ErrorExit(error, icon) {
                  alert(error, scriptName, icon);
                  exit();
              }
              //--------------------------------------------------------------------------------------------------------------------------------------------------------
              

              I use a global variable set (type of Object) which contains all settings taken from the dialog box and used for saving, restoring and throughout the script. Handling one variable is much easier than, say, 100. And it's a good scripting practice to use as little global variables as possible.

              Note that in the script I save settings within the Application object so they are available for all documents:

              app.insertLabel("Kas_" + scriptName, set.toSource());
              

              Alternatively, you can save them within a Document object so each document will have its own settings:

              app.activeDocument.insertLabel("Kas_" + scriptName, set.toSource());
              

               

              If you're using a modal dialog box, use window's onClose method instead.

              • 4. Re: Script-UI and "Reset button"
                Obi-wan Kenobi Adobe Community Professional

                Hi Kas,

                 

                Nice to read you since such a long time! 

                 

                That seems very cool! I'm working on it!

                 

                Thanks! [ see you soon!  ]

                 

                (^/)

                • 5. Re: Script-UI and "Reset button"
                  Kasyan Servetsky Level 5

                  Here's a much more complex example taken from one of my recent scripts. It's a modal dialog box imitating the way the InDesign's Find-Change dialog box works:

                  9-4-2016 10-15-41 AM.png

                  #targetengine "Kasyan"
                  NullifyGlobals();
                  var set, doc, selectedStory, selectedText, selectedPreset, presetsFolder, presetsList, scope, allParStyles, allParStylesNames, allCharStyles, allCharStylesNames, lastSet, style, w, startPageNum,
                  scriptName = "Apply styles based on tag hierarchy 4.1",
                  glueFile = new File(app.filePath + "/Scripts/xml rules/glue code.jsx"),
                  f = 0,
                  textArr = [],
                  foundAll = false;
                  
                  PreCheck();
                  app.doScript(glueFile, ScriptLanguage.JAVASCRIPT);
                  
                  //===================================== FUNCTIONS  ======================================
                  function CreateDialog() {
                      var preferredWidth = 160,
                      unsolicitedCall = false;
                      
                      GetPresetNames();
                      GetDialogSettings();
                      
                      w = new Window("palette", scriptName);
                      w.orientation = "row";
                      if (set.location != null) {
                          w.location = set.location;
                      }
                      w.onClose = function() {
                          var tmp = set; // for dubugging only
                          set.preset = [w.g1.g.ddl.selection.index, w.g1.g.ddl.selection.text]; // preset
                          set.location = String(w.location);
                          set.zoom = w.g2.p.et.text;
                          
                          if (w.g1.p1.rb0.value == true) {
                              set.useXmlRules = true;
                          }
                          else {
                              set.useXmlRules = false;
                          }
                      
                          app.insertLabel("Kas_" + scriptName, set.toSource());
                      }
                  
                      w.g1 = w.add("group");
                      w.g1.orientation = "column";
                      
                      // Preset --------------------------------------------------------------------------------------------------------------
                      w.g1.g = w.g1.add("group");
                      w.g1.g.orientation = "row";
                      w.g1.g.alignment = "fill";
                      w.g1.g.st = w.g1.g.add("statictext", undefined, "Preset:");
                      w.g1.g.ddl = w.g1.g.add("dropdownlist", undefined, presetsList);
                      w.g1.g.ddl.selection = selectedPreset;
                      w.g1.g.ddl.minimumSize = [50, undefined];
                      w.g1.g.ddl.onChange = function() {
                          //$.writeln("Entering: w.g1.g.ddl.onChange");
                          if (this.selection == null) return; // just added a new preset - no selection
                          if (unsolicitedCall) return; // called by "onChange" from an edit text field
                          //$.writeln("this.selection.index = " + this.selection.index);
                          if (this.selection.index == 0) { // 0 - Last Saved Settings, 1 - separator is never selected
                              if (lastSet != null) {
                                  set = lastSet;
                                  UpdateSettings();
                              }
                              else { // lastSet = null - get saved settings
                                  GetDialogSettings();
                                  UpdateSettings();
                              }
                          }
                          else { //  2+ - a custom preset is selected
                              var presetFileName = presetsList[w.g1.g.ddl.selection.index] + ".txt";
                              var presetFile = new File(presetsFolder.fsName + "/" + presetFileName);
                              if (presetFile.exists) {
                                  presetFile.open("r");
                                  var text = presetFile.read(); 
                                  presetFile.close();
                                  var rememberPreset = set.preset; // preset is not saved in preset so remember it...
                                  set = eval(text);
                                  set.preset = rememberPreset; // ... and restore after reading preset settings
                                  UpdateSettings();
                              }
                              else {
                                  alert("Preset file \"" + presetFileName + "\" doesn't exist", scriptName, true);
                                  return;
                              }
                          }
                      }
                      
                      var iconSavePreset = "\u0089PNG\r\n\x1A\n\x00\x00\x00\rIHDR\x00\x00\x00\x12\x00\x00\x00\x12\b\x06\x00\x00\x00V\u00CE\u008EW\x00\x00\x00\x04sBIT\b\b\b\b|\bd\u0088\x00\x00\x00\tpHYs\x00\x00\x0B\x12\x00\x00\x0B\x12\x01\u00D2\u00DD~\u00FC\x00\x00\x00\x1CtEXtSoftware\x00Adobe Fireworks CS6\u00E8\u00BC\u00B2\u008C\x00\x00\x00\x18tEXtCreation Time\x0019.10.2012\u00AA\x15q\x18\x00\x00\x01\u008DIDAT8\u008D\u00B5R=k\x02A\x10\u009D\x15=\u00A2{\u00C5ETHq~\x14\u0091\u00FC\u0082\u0090\u00DE\u00CA\u00CA\u00C6\u0083@\u00CAtB\u00EEgHj+\u009Bti\u00FC\x11b\x1BR\u00A4\u0088M4\u0082\u00A8D\u00C8z\u009CW\u00F811r\u0093B\u00CF\u00C8\u00F9-\u00C9\u0083\u0081a\u00E7\u00ED\u009B7\u00B3\x0B\u00F0G`\u00BB\b\u0091H\u00E4\u00CD\u00C9\u0085\x10\x17\u009Bx\u00DE]B\u00B2,\u00F7\u0097\u00846\u00F2<\u00BB\u0084\u00F6\u00C5\u00DA\u00D1\u00C2\u00E1\u00F0\u00FB\u0092#\u00CB\u00C9\x07\u0083\u0081\u00E2\u00E4\u00BD^\u00EF|\u00AB#\"\u0092\u0084\x10z0\x18\u009Cr\u00CEGD$9\u00C19\x1F\u0085B![\b\u00A1\u00EF\u00E5\u0088\u0088N\x01 \u0095L&\u00EF'\u0093\u00C9\u00B7s.I\u0092\u00AF^\u00AF\u00DF\x01@\u009916\u00D9>\u00EC\u0092\x18\x11e\x13\u0089DKU\u00D5F<\x1Eo\x12Qv\u00DEd\x05\f\x00 \x16\u008B\u0095\x01\u00E0\u00D2]T\x14\u0085\u00A5\u00D3\u00E9f\u00A9T:\u00CBd2\u00D3J\u00A5\"[\u0096E.\u00DAs\u00AB\u00D5J1\x00\u0080h4j\x14\n\u0085\u0087u\u009D\u00FC~\u00FF\u0089\u00AA\u00AA1\u00C30>M\u00D3\u00B4\u00DCu]\u00D7o\u00DB\u00EDv\u00C8Y\u00F6\x07\"\u00DA\u00B9\\\u00EE\x1A\x11\u00ED\u00E5\u00E8\u00F7\u00FB\u00A3Z\u00AD\u00D6\u00E8v\u00BB\u00A6\u00BB\u0086\u00886\x00|\x00\u00FC~HBD;\u009F\u00CF?\"\u00E2\u008A+D\u00FCZ\u00E7\u00D6\u00B9\u00BB\x10\"\"\u00EF\\\u00FD`\x10\u0091w!\x14\b\x04|\u00E3\u00F1\u00D8\u00BD\u00C4\u00BD\u00A0(\u008A\u00D4\u00E9tfB\u009A\u00A6\u00BD\x16\u008B\u00C5\u009B\u00E1px\u0090+\u00CE\u00B9G\u00D3\u00B4\u0097j\u00B5:{~\"\u00BA\x02\u0080\u00C41\u008E\x00\u00A0\u00C9\x18{:\u00F2\u00EE?\u00E2\x07\u00B4\u0080\u00CBu\u00AEO\u00F6q\x00\x00\x00\x00IEND\u00AEB`\u0082";
                      w.g1.g.savePreset = w.g1.g.add("iconbutton", undefined, iconSavePreset);
                      w.g1.g.savePreset.onClick = function() {
                          if (w.g1.p1.et.text == "") {
                              alert ("Please fill in all the text edit fields. No empty fields are allowed.", "Error", true);
                          }
                          else {
                              var presetName = prompt("Enter a name for new preset", "", "Save Preset");
                              if (presetName != null && presetName != "") {
                                  var presetFile = new File(presetsFolder.fsName + "/" + presetName + ".txt");
                                  if (!presetFile.exists) { // New preset
                                      WriteToFile(GetSelectedSettings(), presetFile);
                                      w.g1.g.ddl.removeAll();
                                      GetPresetNames();
                                      for (var i = 0; i < presetsList.length; i++) {
                                          //$.writeln(i + " - " - presetsList[i]);
                                          w.g1.g.ddl.add((i==1) ? "separator" : "item", presetsList[i]);
                                      }
                                      
                                      var index = GetArrayIndex(presetsList, presetName);
                                      unsolicitedCall = true;
                                      if (index != null) {
                                          w.g1.g.ddl.selection = index;
                                      }
                                      else {
                                          w.g1.g.ddl.selection = 0;
                                      }
                                      unsolicitedCall = false;
                                  }
                                  else { // Overwrite the existing preset
                                      if (confirm("File \"" + presetName + "\" already exists. Do you want to overwrite it?", false, "Warning")) {
                                          WriteToFile(GetSelectedSettings(), presetFile);
                                      }
                                  }
                              }
                          }
                      }
                      // xPath --------------------------------------------------------------------------------------------------------------
                      w.g1.p1 = w.g1.add("panel", undefined, "xPath:");
                      w.g1.p1.orientation = "column";
                      w.g1.p1.alignment = "fill";
                      w.g1.p1.alignChildren = "left";
                  
                      w.g1.p1.et = w.g1.p1.add("edittext", undefined, set.xPath);
                      w.g1.p1.et.alignment = "fill";
                      w.g1.p1.et.onChange = function() {
                          if (this.text == "") {
                              alert("The \"xPath\" text field should not be empty.", "Error", true);
                              this.text = set.xPath;
                          }
                          else {
                              set.xPath = this.text;
                              ResetPreset();
                          }
                      }
                  
                      w.g1.p1.rb0 = w.g1.p1.add("radiobutton", undefined, "Use XML rules");
                      w.g1.p1.rb0.onClick = function() {
                          set.useXmlRules = true;
                      }
                  
                      w.g1.p1.rb1 = w.g1.p1.add("radiobutton", undefined, "Use evaluate xPath expression");
                      w.g1.p1.rb1.onClick = function() {
                          set.useXmlRules = false;
                      }
                  
                      if (set.useXmlRules) {
                          w.g1.p1.rb0.value = true;
                      }
                      else {
                          w.g1.p1.rb1.value = true;
                      }
                  
                      // Clear overrides --------------------------------------------------------------------------------------------------------------
                      w.g1.p2 = w.g1.add("panel", undefined, "Clear overrides:");
                      w.g1.p2.orientation = "column";
                      w.g1.p2.alignment = "fill";
                      w.g1.p2.alignChildren = "left";
                      
                      w.g1.p2.rb0 = w.g1.p2.add("radiobutton", undefined, "Don't clear overrides");
                      w.g1.p2.rb0.onClick = UpdateOverrides;
                      w.g1.p2.rb1 = w.g1.p2.add("radiobutton", undefined, "All");
                      w.g1.p2.rb1.helpTip = "Remove paragraph and character formatting";
                      w.g1.p2.rb1.onClick = UpdateOverrides;
                      w.g1.p2.rb2 = w.g1.p2.add("radiobutton", undefined, "Character-level overrides only");
                      w.g1.p2.rb2.helpTip = "Remove character overrides, but preserve paragraph formatting overrides";
                      w.g1.p2.rb2.onClick = UpdateOverrides;
                      w.g1.p2.rb3 = w.g1.p2.add("radiobutton", undefined, "Paragraph-level overrides only");
                      w.g1.p2.rb3.helpTip = "Remove paragraph-level overrides, but preserve character-level overrides";
                      w.g1.p2.rb3.onClick = UpdateOverrides;
                      
                      if (set.rbSelOverrides == 0) {
                          w.g1.p2.rb0.value = true;
                      }
                      else if (set.rbSelOverrides == 1) {
                          w.g1.p2.rb1.value = true;
                      }
                      else if (set.rbSelOverrides == 2) {
                          w.g1.p2.rb2.value = true;
                      }    
                      else if (set.rbSelOverrides == 3) {
                          w.g1.p2.rb3.value = true;
                      }
                  
                      w.g1.p3 = w.g1.add("panel", undefined, "Apply styles:");
                      w.g1.p3.orientation = "column";
                      w.g1.p3.alignment = "fill";
                      w.g1.p3.alignChildren = "left";
                  
                      // Paragraph styles --------------------------------------------------------------------------------------------------------------
                      w.g1.p3.g1 = w.g1.p3.add("group");
                      w.g1.p3.g1.orientation = "row";
                      
                      w.g1.p3.g1.cb = w.g1.p3.g1.add("checkbox", undefined, "Paragraph");
                      w.g1.p3.g1.cb.value = set.applyParStyle;
                      w.g1.p3.g1.cb.onClick = function() {
                          if (this.value) {
                              w.g1.p3.g1.ddl.enabled = true;
                              w.g1.p3.g2.cb.enabled = true;
                              set.applyParStyle = true;
                          }
                          else {
                              w.g1.p3.g1.ddl.enabled = false;
                              w.g1.p3.g2.cb.enabled = false;
                              set.applyParStyle = false;
                          }
                          ResetPreset();
                      }
                  
                      w.g1.p3.g1.ddl = w.g1.p3.g1.add("dropdownlist", undefined, allParStylesNames);
                      w.g1.p3.g1.ddl.selection = set.parStyle[0];
                      w.g1.p3.g1.ddl.preferredSize.width = preferredWidth;
                      w.g1.p3.g1.ddl.onChange = function() {
                          set.parStyle = [this.selection.index, this.selection.text];
                          ResetPreset();
                      }
                  
                      w.g1.p3.g2 = w.g1.p3.add("group");
                      w.g1.p3.g2.orientation = "row";
                      
                      w.g1.p3.g2.cb = w.g1.p3.g2.add("checkbox", undefined, "Clearing overrides");
                      w.g1.p3.g2.cb.value = set.clearingParStyleOverrides;
                      w.g1.p3.g2.cb.alignment = "Clear any text attributes before applying the style";
                      
                      if (w.g1.p3.g1.cb.value) {
                          w.g1.p3.g1.ddl.enabled = true;
                          w.g1.p3.g2.cb.enabled = true;
                          clearingParStyleOverrides = true;
                      }
                      else {
                          w.g1.p3.g1.ddl.enabled = false;
                          w.g1.p3.g2.cb.enabled = false;
                          clearingParStyleOverrides = false;
                      }    
                      
                      w.g1.p3.p = w.g1.p3.add("panel", undefined, ""); // separator
                      w.g1.p3.p.alignment = "fill";
                      w.g1.p3.p.size = [undefined, 0];
                  
                      // Character styles --------------------------------------------------------------------------------------------------------------
                      w.g1.p3.g3 = w.g1.p3.add("group");
                      w.g1.p3.g3.orientation = "row";
                      
                      w.g1.p3.g3.cb = w.g1.p3.g3.add("checkbox", undefined, "Character");
                      w.g1.p3.g3.cb.value = set.applyCharStyle;
                      w.g1.p3.g3.cb.onClick = function() {
                          if (this.value) {
                              w.g1.p3.g3.ddl.enabled = true;
                              set.applyCharStyle = true;
                          }
                          else {
                              w.g1.p3.g3.ddl.enabled = false;
                              set.applyCharStyle = false;
                          }
                          ResetPreset();
                      }
                  
                      w.g1.p3.g3.ddl = w.g1.p3.g3.add("dropdownlist", undefined, allCharStylesNames);
                      w.g1.p3.g3.ddl.selection = set.charStyle[0];
                      w.g1.p3.g3.ddl.preferredSize.width = preferredWidth;
                      w.g1.p3.g3.ddl.onChange = function() {
                          set.charStyle = [this.selection.index, this.selection.text];
                          ResetPreset();
                      }
                      
                      if (w.g1.p3.g3.cb.value) {
                          w.g1.p3.g3.ddl.enabled = true;
                      }
                      else {
                          w.g1.p3.g3.ddl.enabled = false;
                      }
                      
                      // Scope --------------------------------------------------------------------------------------------------------------
                      w.g1.p4 = w.g1.add("panel", undefined, "Process:");
                      w.g1.p4.orientation = "column";
                      w.g1.p4.alignment = "fill";
                      w.g1.p4.alignChildren = "left";
                      
                      w.g1.p4.rb0 = w.g1.p4.add("radiobutton", undefined, "active document");
                      w.g1.p4.rb0.onClick  = UpdateScope;
                      w.g1.p4.rb1 = w.g1.p4.add("radiobutton", undefined, "the selected story");
                      w.g1.p4.rb1.onClick  = UpdateScope;
                      w.g1.p4.rb2 = w.g1.p4.add("radiobutton", undefined, "the selected text");
                      w.g1.p4.rb2.onClick  = UpdateScope;
                  
                      w.g1.p4.rb0.value = true;
                      
                      if (selectedStory == undefined) {
                          w.g1.p4.rb1.enabled = false;
                          w.g1.p4.rb2.enabled = false;
                      }
                  
                      w.g2 = w.add("group", undefined, "");
                      w.g2.orientation = "column";
                      w.g2.alignment = "top";
                      
                      // Zoom --------------------------------------------------------------------------------------------------------------
                      w.g2.p = w.g2.add("panel", undefined, "Zoom:");
                      w.g2.p.et = w.g2.p.add("edittext", undefined, set.zoom);
                      w.g2.p.et.characters = 3;
                      w.g2.p.et.onChange = function() {
                          if (this.text == "") {
                              alert("The \"Zoom\" text field should not be empty.", "Error", true);
                              this.text = String(set.zoom);
                          }
                          else if (isNaN(this.text)) {
                              alert("\"Zoom\" should be a number.", "Error", true);
                              this.text = String(set.zoom);
                          }
                          else {
                              set.zoom = Number(this.text);            
                          }
                      }
                  
                      // Buttons --------------------------------------------------------------------------------------------------------------
                      var buttonWidth = 80;    
                      w.g2.b1 = w.g2.add("button", undefined, ((textArr.length == 0) ? "Find" : "Find Next"));
                      w.g2.b1.preferredSize.width = buttonWidth;
                      w.g2.b1.onClick = function() {
                          0
                          if (w.g1.p1.et.text == "") {
                              alert("The \"xPath\" text field should not be empty.", "Error", true);
                          }
                          else if (!foundAll) {
                              FindAll();
                              IterateFinds();
                          }
                          else {
                              IterateFinds();
                          }
                      }
                      
                      w.g2.b2 = w.g2.add("button", undefined, "Change");
                      w.g2.b2.preferredSize.width = buttonWidth;
                      w.g2.b2.enabled = false;
                      w.g2.b2.onClick = ChangeItem;
                      
                      w.g2.b3 = w.g2.add("button", undefined, "Change All");
                      w.g2.b3.preferredSize.width = buttonWidth;
                      w.g2.b3.onClick = ChangeAll;
                      
                      w.g2.b4 = w.g2.add("button", undefined, "Change/Find");
                      w.g2.b4.preferredSize.width = buttonWidth;
                      w.g2.b4.enabled = false;
                      w.g2.b4.onClick = IterateFindsChange;
                      
                      w.g2.b5 = w.g2.add("button", undefined, "Close");
                      w.g2.b5.preferredSize.width = buttonWidth;
                      w.g2.b5.onClick = function() {
                          w.close();
                      }
                      
                      var iconOverset =  "\u0089PNG\r\n\x1A\n\x00\x00\x00\rIHDR\x00\x00\x00\f\x00\x00\x00\f\b\x06\x00\x00\x00Vu\\\u00E7\x00\x00\x00\x04sBIT\b\b\b\b|\bd\u0088\x00\x00\x00\tpHYs\x00\x00\x0B\x12\x00\x00\x0B\x12\x01\u00D2\u00DD~\u00FC\x00\x00\x00\x15tEXtCreation Time\x001/23/13W\x7F\u00B5\u008B\x00\x00\x00\x1CtEXtSoftware\x00Adobe Fireworks CS6\u00E8\u00BC\u00B2\u008C\x00\x00\x00\u008DIDAT(\u0091\u00CD\u008E9\x0E\u00C2@\fE\u009FQ*\x1A\u00CAh:\u00A4@\u00C3\x05\u00E6\u00FC)\u00E7\x024\u0090z\u00C4\x05\x12H\x10N\u00C1\x18e!\u008A\u00E8\u00B0d\u00C9\u00CB\x7F\u00FE\u0086\x1FC\x00\u00F4\u0080\u00AE\n\u00AFom\x06P\u00BF`[\u00E9mI\\\x17\u0092[\u00BD1\x00\u00E8,\u0083\u0093|\u00D8\u00A7=#\x07\u00E099\u00FC\u00E9g@\u0093\x1C\u0082\u0093\u00A3-\u0082\u0093=\u0080\u008Fzi\x16\x1C:\x1F\u00F5\u009C\u00C4'\u00AB\u00BF:\u00A4A;y\u00A9]\x03\x1E6\u00F4Q\u00C3\u0090\u009C\x01\u00E5\x1D\u00CA\u009Dx\u00FE\"ze\u00C9?\u00DA\u0083]\u00D8x\x00\x00\x00\x00IEND\u00AEB`\u0082" ;
                      w.g2.i = w.g2.add("image", undefined, iconOverset);
                      w.g2.i.visible = false;
                      w.g2.i.helpTip = "The story containing the found text is overset";
                  
                      UpdateScope(); // also updates buttons - should be after them
                      
                      var showDialog = w.show();
                  
                      // ============ Functions for the dialog box only ============
                      function UpdateOverrides() { // when dialog box is loaded or user clicks a radio button, update the variable
                          if (w.g1.p2.rb0.value == true) { // Don't clear overrides
                              rbSelOverrides = 0;
                          }
                          else if (w.g1.p2.rb1.value == true) { // All
                              rbSelOverrides = 1;
                          }
                          else if (w.g1.p2.rb2.value == true) { // Character-level overrides only
                              rbSelOverrides = 2;
                          }
                          else if (w.g1.p2.rb3.value == true) { // Paragraph-level overrides only
                              rbSelOverrides = 3;
                          }
                      
                          ResetPreset();
                          
                          //$.writeln("rbSelOverrides = " + rbSelOverrides);
                      }
                      //------------------------------------------------------------------
                      function UpdateScope() { // scope is a global var; when dialog box is loaded or user clicks a radio button, update the variable
                          if (w.g1.p4.rb0.value) { // active document
                              scope = 0;
                              w.g2.b1.enabled = true;
                          }
                          else if (w.g1.p4.rb1.value) { // selected story
                              scope = 1;
                              //w.g2.b1.enabled = w.g2.b2.enabled = w.g2.b4.enabled = false;
                          }
                          else if (w.g1.p4.rb2.value) { // selected text
                              scope = 2;
                              w.g2.b1.enabled = w.g2.b2.enabled = w.g2.b4.enabled = false;
                          }
                          //$.writeln("scope = " + scope);
                      }
                      //------------------------------------------------------------------
                      function GetSelectedSettings() {
                          with (set) {
                              xPath = w.g1.p1.et.text; // xPath
                              
                              // Overrides
                              if (w.g1.p2.rb0.value == true) { // Don't clear overrides
                                  rbSelOverrides = 0;
                              }
                              else if (w.g1.p2.rb1.value == true) { // All
                                  rbSelOverrides = 1;
                              }
                              else if (w.g1.p2.rb2.value == true) { // Character-level overrides only
                                  rbSelOverrides = 2;
                              }
                              else if (w.g1.p2.rb3.value == true) { // Paragraph-level overrides only
                                  rbSelOverrides = 3;
                              }
                          
                              applyParStyle = w.g1.p3.g1.cb.value; // Paragraph style
                              parStyle = [w.g1.p3.g1.ddl.selection.index, w.g1.p3.g1.ddl.selection.text];
                              clearingParStyleOverrides = w.g1.p3.g2.cb.value;
                              
                              applyCharStyle = w.g1.p3.g3.cb.value; // Character style
                              charStyle = [w.g1.p3.g3.ddl.selection.index, w.g1.p3.g3.ddl.selection.text];        
                          }
                      
                          delete set.preset;
                          delete set.location;
                          delete set.useXmlRules;
                          var tmp = set; // for debugging only
                          return set.toSource();
                      }
                      //------------------------------------------------------------------
                      function UpdateSettings() {
                          //$.writeln("Entering: UpdateSettings");
                          with (set) {
                              w.g1.p1.et.text = set.xPath; // xPath
                              
                              // Overrides
                              if (rbSelOverrides == 0) { // Don't clear overrides
                                  w.g1.p2.rb0.value = true;
                                  w.g1.p2.rb1.value = w.g1.p2.rb2.value = w.g1.p2.rb3.value = false;
                              }
                              else if (rbSelOverrides == 1) { // All
                                  w.g1.p2.rb1.value = true;
                                  w.g1.p2.rb0.value = w.g1.p2.rb2.value = w.g1.p2.rb3.value = false;
                              }
                              else if (rbSelOverrides == 2) { // Character-level overrides only
                                  w.g1.p2.rb2.value = true;
                                  w.g1.p2.rb0.value = w.g1.p2.rb1.value = w.g1.p2.rb3.value = false;
                              }
                              else if (rbSelOverrides == 3) { // Paragraph-level overrides only
                                  w.g1.p2.rb3.value = true;
                                  w.g1.p2.rb0.value = w.g1.p2.rb1.value = w.g1.p2.rb2.value = false;
                              }
                          
                              w.g1.p3.g1.cb.value = applyParStyle; // Paragraph style
                              w.g1.p3.g2.cb.value = clearingParStyleOverrides;
                              w.g1.p3.g3.cb.value = applyCharStyle; // Character style
                              
                              CheckParStyle();
                              w.g1.p3.g1.ddl.selection = set.parStyle[0];
                              
                              CheckCharStyle();
                              w.g1.p3.g3.ddl.selection = set.charStyle[0];
                  
                          }
                  0            
                          if (useXmlRules == undefined) { // just incase, not sure if it's necessary
                              useXmlRules = true;
                          }    
                      }
                      //------------------------------------------------------------------
                      function ResetPreset() {
                          unsolicitedCall = true;
                          w.g1.g.ddl.selection = 0;
                          unsolicitedCall = false;
                      }
                      // ============ END of functions for the dialog box only ============
                  } // END of CreateDialog
                  //--------------------------------------------------------------------------------------------------------------------------------------------------------
                  

                  Here it reads the saved settings

                  function GetDialogSettings() {
                      var tmp = set; // for dubugging only    
                      set = eval(app.extractLabel("Kas_" + scriptName));
                      var tmp = set; // for dubugging only
                  0    
                      if (set != undefined && set.preset[0] > 1) { // Custom preset
                          if (presetsList[set.preset[0]] == set.preset[1]) { // the name of the preset is the same (no presets were added or deleted)
                              var location = set.location; // remember location - it's not saved in preset
                              var rememberPreset = set.preset;
                              var presetFileName = presetsList[set.preset[0]] + ".txt";
                              var presetFile = new File(presetsFolder.fsName + "/" + presetFileName);
                  
                              if (presetFile.exists) {
                                  presetFile.open("r");
                                  var text = presetFile.read(); 
                                  presetFile.close();
                                  var rememberZoom = set.zoom; // the next line strips off zoom
                                  set = eval(text);
                                  set.zoom = rememberZoom;
                                  set.preset = rememberPreset;
                                  selectedPreset = set.preset[0];
                                  if (location != undefined) {
                                      set.location = location; // get back "set.location" from var
                                  }
                              }
                          }
                      }    
                      
                      0
                      if (set == undefined) {
                          set = {preset: [0, "Last Saved Settings"], xPath: "", useXmlRules: true, rbSelOverrides: 0, parStyle: [1, "[No Paragraph Style]"], applyParStyle: true, clearingParStyleOverrides: false, applyCharStyle: false, charStyle: [0, "[None]"], zoom: 100, location: null};
                      }
                  
                      tmp = set; // for dubugging only
                  
                      if (location != null) {
                          set.location = set.location.split(","); // location is array of numbers
                          set.location[0] = Number(set.location[0]);
                          set.location[1] = Number(set.location[1]);
                      }
                  
                      CheckParStyle();
                      CheckCharStyle();
                  
                      if (allCharStylesNames[set.charStyle[0]] != set.charStyle[1]) {
                          set.charStyle = [0, "[None]"]; // reset to None
                      }
                  
                      set.zoom = Number(set.zoom);
                  
                      if (isNaN(set.zoom) || set.zoom == 0) { // if something goes, wrong reset to 100%
                          set.zoom = 100;
                      }
                  
                      if (selectedPreset == undefined) {
                          selectedPreset = 0;
                      }
                  
                      if (set.useXmlRules == undefined) {
                          set.useXmlRules = true;
                      }
                  
                      tmp = set; // for dubugging only
                  }
                  //--------------------------------------------------------------------------------------------------------------------------------------------------------
                  function CheckParStyle() {
                  //~     $.writeln("Entering: CheckParStyle");
                      if (allParStylesNames[set.parStyle[0]] == undefined || allParStylesNames[set.parStyle[0]] != set.parStyle[1]) {  // if number of styles is less than in the previous doc, or the style name doesn't match
                          var parStyleByName = GetStyleByName(set.parStyle[1], allParStylesNames, allParStyles);
                          
                          if (parStyleByName != null) { // get style by name
                              set.parStyle = parStyleByName;
                          }
                          else if (set.applyParStyle && set.parStyle[1] != "[No Paragraph Style]" && set.parStyle[1] != "[Basic Paragraph]") { // not found, reset to defaults
                              alert("Paragraph style '" + set.parStyle[1] + "' is missing in the active document so '[No Paragraph Style]' will be selected in the 'Paragraph' list.", scriptName, false);
                  
                              set.parStyle = [1, "[Basic Paragraph]"]; // reset to [Basic Paragraph]
                              selectedPreset = 0;
                              
                              if (w != null) { // called from presets ddl.onChange() - reset to last settings if the style used in preset is missing
                                  w.g1.g.ddl.selection = selectedPreset;
                              }
                          }
                      }    
                  }
                  //--------------------------------------------------------------------------------------------------------------------------------------------------------
                  function CheckCharStyle() {
                  //~     $.writeln("Entering: CheckCharStyle");
                      if (allCharStylesNames[set.charStyle[0]] == undefined || allCharStylesNames[set.charStyle[0]] != set.charStyle[1]) {
                          var charStyleByName = GetStyleByName(set.charStyle[1], allCharStylesNames, allCharStyles);
                          
                          if (charStyleByName != null) { // get style by name
                              set.charStyle = charStyleByName;
                          }
                          else if (set.applyCharStyle && set.charStyle[1] != "[None]") { // not found, reset to defaults
                              alert("Character style '" + set.charStyle[1] + "' is missing in the active document so '[None]' will be selected in the 'Character' list.", scriptName, false);
                  
                              set.charStyle = [1, "[None]"]; // reset to [None]
                              selectedPreset = 0;
                              
                              if (w != null) { // called from presets ddl.onChange() - reset to last settings if the style used in preset is missing
                                  w.g1.g.ddl.selection = selectedPreset;
                              }
                          }
                      }    
                  }
                  //--------------------------------------------------------------------------------------------------------------------------------------------------------
                  function GetStyleByName(name, namesList, styles) {
                      var result = null,
                      start = (styles[0].constructor.name == "CharacterStyle") ? 1 : 2; // Skip [None] for char styles, and [No style] and [Basic par style] for par styles
                      
                      for (var i = start; i < namesList.length; i++) {
                          //$.writeln("namesList[" + i + "] = " + namesList[i]);
                          if (namesList[i] == name) {
                              result = [i, namesList[i]];
                  //~             $.writeln("Done! namesList[" + i + "] == name - " + name);
                          }
                      }
                  
                      return result;
                  }
                  //--------------------------------------------------------------------------------------------------------------------------------------------------------
                  function PreCheck() {
                      if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);
                      doc = app.activeDocument;
                      if (doc.converted) ErrorExit("The current document has been modified by being converted from older version of InDesign. Please save the document and try again.", true);
                      if (!doc.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);
                      if (!glueFile.exists) ErrorExit("\"glue code.jsx\" should be located in the \"Scripts > XML rules\" folder for this script to work.", true);
                      
                      GetSelectedStory();
                      
                      allParStyles = doc.allParagraphStyles;
                      allParStylesNames = ["Break Link to Style"];
                      for (var i = 1; i < allParStyles.length; i++) allParStylesNames.push(allParStyles[i].name);
                      
                      allCharStyles = doc.allCharacterStyles;
                      allCharStylesNames = [];
                      for (var k = 0; k < allCharStyles.length; k++) allCharStylesNames.push(allCharStyles[k].name);
                      
                      var scriptFile = GetScriptPath();
                      var scriptFolder = scriptFile.parent;
                      presetsFolder = new Folder( scriptFolder.fsName + "/" + scriptName.replace(/\s\d+\.\d+/, "") + " - Presets/" );
                      VerifyFolder(presetsFolder);
                      
                      startPageNum = app.layoutWindows[0].activePage.documentOffset;
                  
                      CreateDialog();
                  }
                  
                  • 6. Re: Script-UI and "Reset button"
                    Loic.Aigon Adobe Community Professional

                    Hi all,

                     

                    I tend not to use insert/extract label when it comes to storing such UI settings. Reason why if user rebuilds his preferences, then all your settings are gone. I am used to create a xml file (others could prefer json) that I store somewhere useful inside app.userData.

                     

                    Hereafter the logic

                     

                    var file = File ( app.userData+"/settings.xml" );
                    
                    
                    //read 
                    file.open("r");
                    settings = file.read(); 
                    file.close();
                    
                    
                    
                    
                    //write 
                    file.open("w");
                    file.write(data);
                    file.close();
                    

                     

                     

                    FWIW

                    Loic

                    • 7. Re: Script-UI and "Reset button"
                      Obi-wan Kenobi Adobe Community Professional

                      Kas and Loïc,

                       

                      You don't spare me!

                      It'll take me time to understand what you mean!!

                      … but I'll take it!!!

                       

                      Thanks!

                       

                      (^/)

                      • 8. Re: Script-UI and "Reset button"
                        Kasyan Servetsky Level 5

                        Hi Loic,

                        I always provide default settings like so:

                         

                        set = eval(app.extractLabel("Kas_" + scriptName));
                        if (set == undefined) {
                            set = {cbValue: true, cb1Value: true, cb2Value: true, cb3Value: true, cb4Value: true, cb5Value: true, mainFolderPath: defaultMainFolderPath, cbSaveInDesignFilesValue: true, cbInvisibleModeValue: false, cbOverwriteExistingPdfsValue: false, rbSelected: 0};
                        }
                        
                        

                        Thus they always exist before the dialog box is created: even when the script is run for the first time or after preferences have been rebuilt,

                        Another approach I often use in my scripts is saving dialog settings in presets. (Obviously trashing prefs has no influence on them)

                         

                        —Kas

                        • 9. Re: Script-UI and "Reset button"
                          Obi-wan Kenobi Adobe Community Professional

                          Hi Kas and Loïc,

                           

                          Cool to read your comments! 

                           

                          What i'll try to do is:

                          • When the UI is reopen, we could see the last settings by default (inserted before last closing).

                          • The settings can be saved and loaded (files in a folder located in the same folder as the script.

                           

                          (^/)