2 Replies Latest reply on Nov 5, 2014 10:56 AM by aguisler86

    [JS/CS5] remove empty "phantom" tagged elements in the structure?

    aguisler86 Level 1
      baseFolder = "\\...\\";
      logFile = "c:\\...\\logfile.txt";
      
      
      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
      
      
      logToFile ("\n" + (new Date).toString());
      
      
      listFilesInFolder (baseFolder);
      
      
      //var myDocument = app.activeDocument;
      //var documentPhotoCount = countPhotosOnDocument(myDocument);
      //logToFile(myDocument.name + "\t" + documentPhotoCount);
      
      
      
      
      function listFilesInFolder(path) {
          
          var folder = new Folder(path);
          var files = folder.getFiles();
          
          for (var i = 0; i < files.length; i++) {
      
              var file = files[i];
              
              if (file.name.match(/\.indd/)) {
                  //logToFile(file.name);
      
      
                  try {
                      
                      // Open the document in indesign
                      var doc = app.open(file, false, OpenOptions.OPEN_ORIGINAL);
                      var documentPhotoCount = countPhotosOnDocument(doc);
      
      
                      // pass in the document instead of a file name
                      documentPhotoCount;
                      logToFile(doc.name + "\t" + documentPhotoCount);
      
      
                      // close the indesign doc
                      var closeDoc = doc.close(SaveOptions.NO);
      
      
                  } catch (e) {
                      logToFile("Error in " + file.name + " --- " + e.message);
                  }
              
              }
              listFilesInFolder(file.fsName);
          }
          
      }
      
      
      
      
      function countPhotosOnDocument(elm) {
        var count = 0;
      
      
          for (var i = 0; i < elm.xmlElements.length; i++)  { 
      
      
              XMLelementName=elm.xmlElements[i].markupTag.name.toString();
      
              if( XMLelementName=="photo0" || 
        XMLelementName=="photo1" || 
        XMLelementName=="photo2" || 
        XMLelementName=="photo3" || 
        XMLelementName=="photo4" )
              {
        count++;
              }
              
              count = count + countPhotosOnDocument(elm.xmlElements[i]);
          }
      
        return count;
      
      
      }
      
      
      function logToFile(message) {
      
      
          $.writeln(message);
      
      
          // open log file    
          var log = new File(logFile);
          // Write this message to a log file
          log.open('a');
          log.writeln(message);
          // close log file
          log.close();
          
      }
      
      
      
      
      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
      
      
      

       

      So, I have this script that counts the number of photos on my documents based on their xml tag name. The problem I've ran into is that sometimes we have deleted a photo and added a new one. When this happens, it still leaves a photo tag in the structure and then adds another one when we re-tag the new picture. The script can't differentiate between what xmlelements are actually attached to a photo and which ones are just left over empty ones. I have to run this for over 20,000 templates so is there a way to either A) Remove the empty xmlelements in the stucture or B) Make the script ignore the empty ones while counting the real ones?