10 Replies Latest reply on Dec 8, 2016 12:31 AM by tobiask43046735

    Is it possible to store script settings with InDesign file?

    oleh.melnyk Level 3

      Hi,

       

      I have some crazy idea: is it possible to store script settings with InDesign file?

       

      In that case scripters will be able to store those settings with file, and when script runs - it can check if those setting exists - if so - script can run without showing dialogue with parameters... (as all necessary parameters stored with indesign file), and of course we can force InDesign to show script parameters dialogue if we run it with alt/option key pressed while running this script. Maybe even different scripts will be able to share those settings and interact with each other.. we will be able to have settings that will be saved with file, or temp settings for current document or session (till InDesign is closed). With eventListeners and startup scripts we will be able to do scripts that would be able to check for updates, and automatically execute when we placing images and text, and so on.. this can be a whole InDesign scripting framework, that allow to write scripts that will use same libraries and ui so we don't have to write everything from scratch everytime and just included required libs..

       

      so here is 2 main questions for now:

      1) is it possible to store script settings with .indd file

      2) who is intrested in creation InDesign scripting framework (because this only makes sense if many people will use it)

       

      PS: I know that it's possible to store settings in external file, but this time I'm interested only for saving params with .indd

        • 1. Re: Is it possible to store script settings with InDesign file?
          oleh.melnyk Level 3

          Function syntax for writing label:

           

          app.activeDocument.insertLabel('myLabel', 'myLabelValue');

          IMPORTANT: If we forget label name, label is LOST! Really?!?! YEAH, REALLY! :D

           

          Function syntax for reading label:

          var myLabel = app.activeDocument.extractLabel('myLabel');

          http://indisnip.wordpress.com/2010/07/31/saving-script-data-within-document/

          1 person found this helpful
          • 2. Re: Re: Is it possible to store script settings with InDesign file?
            Laubender Adobe Community Professional & MVP

            No. Not really. Nothing is lost. :-)
            You could crack open an IDML file of the document and find the keys and the values…

             

            It's stored in the designmap.xml part of the IDML file.

             

            <Properties>
                <Label>
                    <KeyValuePair Key="MyKeyString" Value="MyValueString"/>
                </Label>
            </Properties>
            

             

            Uwe

            • 3. Re: Re: Is it possible to store script settings with InDesign file?
              oleh.melnyk Level 3

              ah, of cource InDesign don't know if I remeber variable name, and it store it somewhere... but it's not cool to export file to idml and search in XML those labels..

              I wish I could get them like:

              app.activeDocument.properties.labels.keyValuePair["MyKeyString"];
              

               

              or something like this...

              • 4. Re: Re: Is it possible to store script settings with InDesign file?
                Harbs. Level 6

                It's not so hard to export an IDML. Here's how to get all labels from a snippet:

                 

                Re: is there a way to retrieve all labels inserted in an object?

                1 person found this helpful
                • 6. Re: Re: Is it possible to store script settings with InDesign file?
                  Harbs. Level 6

                  Here's a similar function for extracting the labels from a document:

                   

                  var labels = GetDocLabels(app.documents[0]);
                  
                  function GetDocLabels(doc){
                      var idmlFile = File(Folder.temp + "/temp.idml");
                      doc.exportFile(ExportFormat.INDESIGN_MARKUP,idmlFile);
                      var folder = Folder(Folder.temp + "/temp_idml");
                      app.unpackageUCF(idmlFile,folder);
                      var designMap = File(folder + "/designmap.xml");
                      var valuePairs = GetValuePairs(designMap);
                      return valuePairs;
                     
                  }
                  
                  function GetValuePairs(file){
                      file.open('r'); 
                      var contents = file.read(); 
                      file.close(); 
                      var contXML = XML(contents); 
                      var vals = {} 
                      for each(var pair in contXML..KeyValuePair){ 
                          vals[pair.@Key.toString()] = pair.@Value.toString(); 
                      } 
                      return vals; 
                  } 
                  
                  1 person found this helpful
                  • 7. Re: Re: Re: Is it possible to store script settings with InDesign file?
                    oleh.melnyk Level 3

                    AWESOME!

                    I think it's even possible to do "asynchronous export":

                     

                        doc.asynchronousExportFile(ExportFormat.INDESIGN_MARKUP,idmlFile);

                     

                    and then delete temp file once we finished the parsing process

                         ..
                         idmlFile.remove();
                         return vals;
                    }
                    

                     

                    but all this procedure may take some time((

                    anyway thanks for showing me the possibility of saving and parsing XML, it's really amazing!)

                    • 8. Re: Re: Re: Re: Is it possible to store script settings with InDesign file?
                      oleh.melnyk Level 3

                      Ok, here is the labels parser I end with, actually it works extremely fast!

                      Thanks again, Harbs!

                       

                      var labels = GetDocLabels(app.documents[0]);
                        
                      function GetDocLabels(doc)
                      {  
                          var idmlFile = File(Folder.temp + "/temp.idml");  
                          doc.exportFile(ExportFormat.INDESIGN_MARKUP,idmlFile); // asynchronousExportFile works, but script fails as it try to execute before file fully exported
                          var folder = Folder(Folder.temp + "/temp_idml");  
                          app.unpackageUCF(idmlFile,folder);  
                          var designMap = File(folder + "/designmap.xml");  
                          var valuePairs = GetValuePairs(designMap);  
                          return valuePairs;     
                      }  
                        
                      function GetValuePairs(file)
                      {  
                          file.open("r");   
                          var contents = file.read();   
                          file.close();    
                          var contXML = XML(contents);
                          var vals = {};
                          for each(var pair in contXML..KeyValuePair)
                          {   
                              vals[pair.@Key.toString()] = pair.@Value.toString();        
                          }
                          return vals;   
                      }
                      
                      
                      //> START of DELETE FOLDER Function
                      function deleteFolder(path)
                      {
                          removeFolder = path; // path to folder that need to be removed
                          filesToRemove = removeFolder.getFiles();
                          
                          for(i = 0; i < filesToRemove.length; i++)
                          {    
                              try
                              {
                                  innerFolders = filesToRemove[i].getFiles();
                                  if(innerFolders.length >= 1)
                                  {
                                      for(f = 0; f < innerFolders.length; f++)
                                      {
                                          innerFolders[f].remove()
                                      }
                                  }
                              }
                              catch(e){}
                              
                              filesToRemove[i].remove(); // remove temp files once we done
                          }
                          removeFolder.remove();
                      }//< END of DELETE FOLDER function
                      
                      
                      // remove temp files once we done
                      deleteFolder(Folder(Folder.temp + "/temp_idml"));
                      File(Folder.temp + "/temp.idml").remove(); 
                      
                      
                      // DUMPER FUNCTIONS
                      function dump(o)
                      {
                          out = "";
                          for (var p in o)
                          {         
                              out += p + ": " + o[p] + "\n";
                          }
                          return(out);
                      }
                      
                      
                      function alert_scroll (title, input)
                      {
                         if (input instanceof Array)
                             input = input.join ("\r");
                         var w = new Window ("dialog", title);
                         var list = w.add ("edittext", undefined, input, {multiline: true, scrolling: true});
                         list.maximumSize.height = w.maximumSize.height-100;
                         list.minimumSize.width = 550;
                         w.add ("button", undefined, "Close", {name: "ok"});
                         w.show ();
                      }
                      
                      
                      alert_scroll ("Document Labels", dump(labels)); exit();
                      
                      2 people found this helpful
                      • 9. Re: Re: Re: Re: Is it possible to store script settings with InDesign file?
                        Harbs. Level 6

                        I'm lazy about cleaning up files. The reason I write to the temp folder is because the system is supposed to clean that up on its own. A few idml files does not use any significant disk space, and running the script multiple times will just overwrite the last files.

                         

                        But, I'm the type who always has a mess on my desk as well...

                         

                        Harbs