6 Replies Latest reply on Apr 11, 2017 5:46 AM by cbishop01

    Help to stop this script from Creating New Text frame every time File is opened.

    cbishop01 Level 2

      Hi everyone.  How can i keep this Script from writing overtop of the Our Barcode Text frame i have being created?  What it does is the first time i open the Documnet It places OUR Barcode text frame where it needs to be.  Then I save the file close it.  WHen i reopen the FIle (Any FIle) It places another Text Frame over top of it.  I'm trying to get it to Make the Text frame and if it sees that its already made it will not create a new Frame.

         #targetengine "Indesign CC2017"
         
       app.addEventListener( "afterOpen" , dotFOnPageOne );  
       app.addEventListener( "afterOpen", doMLyer );   
       app.addEventListener( "beforeSave", doLLayer );  
       app.addEventListener( "beforeOpen", doULLayer );
      
      function doULLayer(){
       app.activeDocument.layers.item("OUR Barcode").locked = false;
      
             }
             
      function doLLayer(){
       app.activeDocument.layers.item("OUR Barcode").locked = true;
      
             }
             
          function doMLyer(){
              layers = app.documents[0].layers;  
          if (layers.item('OUR Barcode').isValid) {  
            layers.item('OUR Barcode').move (LocationOptions.AT_BEGINNING); 
          } 
      }
        
      //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CREATE NEW layer<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\\  
          function doLyer(){
              var _d = app.documents[0];  
              var _allStories = _d.stories;  
              for(var n=_allStories.length-1;n>=0;n--){  
              var _storyAllTextFrames = _allStories[n].textContainers;  
                  for(var m=_storyAllTextFrames.length-1;m>=0;m--){ 
                      //If the contents of a text frame is an empty string:  
       if(_storyAllTextFrames[m].contents === ""){  
       _storyAllTextFrames[m].select();  
       //Convert frame to "Unassigned":  
                /*      try{ 
       app.scriptMenuActions.itemByID(11297).invoke();  
       }catch(e){};  */
       };  
       };  
                  };  
                
              //Trick to deselect the last selection  
              //Add a new textFrame, select and remove it:  
              var _tempTextFrame = _d.textFrames.add();  
       _tempTextFrame.select();  
       _tempTextFrame.remove();  
             }
      //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>NEW layer Color Settings<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\\  
          function addlayer(doc, name){  
              try{  
                  var layer = doc.layers.itemByName(name);  
                  if(layer && layer.isValid) {  
       return layer;  
                      } else {  
                  var mylayer = doc.layers.add();  
       mylayer.name = name; 
       mylayer.layerColor = [100,50,100]
                      return mylayer;  
              }  
            } catch(e) {  
           }  
          }  
      
      //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CREATE TEXT FRAME<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\\ 
           function addTextF(doc, layer, name){  
      // ************ how can I get the below to only allow me to select the **OUR Barcode** when its locked? ************* \\   
       app.generalPreferences.preventSelectingLockedItems = false;
                   
              var myBlendingSettings = { blendMode : BlendMode.NORMAL };        
              var myTransparencySettings = { blendingSettings : myBlendingSettings };   
              var tFR = doc.textFrames.itemByName(name);
              
              if(tFR && tFR.isValid) {        
       tFR.remove();  
              }  
              var origUnit = app.scriptPreferences.measurementUnit;  
              app.scriptPreferences.measurementUnit = MeasurementUnits.INCHES;
          tFR = doc.textFrames.add(layer, LocationOptions.UNKNOWN, {name: name, fillColor :"Paper", fillTint: 100, transparencySettings : myTransparencySettings, geometricBounds: ['.504in','.21in','.376in','-.21in'] });
          tFR.rotationAngle += -90;
      // ************ Cant get the below to Fill my pictures Proportionally. ************* \\   
      /*   
       tFR.fit(FitOptions.FILL_PROPORTIONALLY); 
          tFR.resize(  
       CoordinateSpaces.innerCoordinates, 
       AnchorPoint.topLeftAnchor,  
       ResizeMethods.MULTIPLYING_CURRENT_DIMENSIONS_BY,  
              [.40,.40]  
              );  
            
       tFR.fit(FitOptions.CENTER_CONTENT); 
      */    
       tFR.locked=true;
       app.scriptPreferences.measurementUnit = origUnit;      
                    return tFR;  
                    
          }  
      
              // VERTICAL  geometricBounds: ['.65in','.062in','.23in','-.066'] }); 
              /* HORIZONTAL  geometricBounds: ['.333','.124','.46','-.296'] }); */
       //geometricBounds: ['.333in','.21in','.20501in','-.296in'] });
              // geometricBounds: ['.504in','.21in','.376in','-.21in'] });
      
            
      
      //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>MOVE TEXT FRAME TO layer<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\\ 
          function dotFOnPageOne(event){       
              var doc = event.target;     
              if(!(doc && doc.constructor.name == "Document")) {  
       return;  
              }  
                  
              var mylyer = addlayer(doc, "OUR Barcode");  
              var tFR = addTextF(doc, mylyer, "OUR Barcode"); 
      
       }