6 Replies Latest reply on Aug 19, 2015 3:29 PM by smoothsea

    Checking if an object exists on a page

    smoothsea Level 3

      Hi,

       

      I'm trying to automate adding commonly-used elements on existing documents and I want to be able to check if an object already exists on the page so that it doesn't get added twice. I'm pretty new to JavaScript but pretty confident in Indesign CC.

       

      Here is the script fragment that I want to do the job:

       

       

      // add address window
      if(!doesObjectExist(myAddressWindow)){
          var myAddressWindow=myDocument.pages.item(0).textFrames.add();
          var myCorners=CornerOptions.ROUNDED_CORNER;
          myAddressWindow.properties={name:"myAddressWindow",geometricBounds:["33mm", "20mm", "78mm", "110mm"],strokeWeight:0.5,strokeColor:"Magenta",topLeftCornerOption:myCorners,topRightCornerOption:myCorners,bottomLeftCornerOption:myCorners,bottomRightCornerOption:myCorners};
      }
      
      
      function doesObjectExist(objectName){
       with(myDocument.pages.everyItem){
           if(name==objectName){
               return true;
               }
           }
      

       

      the myAddressWindow element always get added, even if it already exists and I can't figure out why.

       

      Thanks and regards,

       

      Malcolm

        • 1. Re: Checking if an object exists on a page
          Vamitul Level 4

          Please post your entire script and let's see if we can make it work. Your current snippet makes little to no sense.

          • 2. Re: Checking if an object exists on a page
            Laubender Adobe Community Professional & MVP

            Hi Malcom,

            you could use itemByName() on the pageItems in the document, if that element you are looking for is not nested ( inside a group or inside a graphic frame or anchored to a text frame etc.pp).:

             

            testSomething("TheNameOfTheThing");
            
            function testSomething(/*String*/myName){
               
                if( app.documents[0].pageItems.itemByName(myName).isValid ){
                    //Nothing to do:
                    $.writeln("Nothing to do.")
                    return;
                    };
                else{ doSomething() };
               
                };
            
            function doSomething(){
                /* Your code for adding something should follow here: */
                $.writeln("Build something.");
                };
            

             

            You could also test for is not valid, and do something, if true.

            The above code is just an exmple not optimized for anything.

             

            Uwe

            • 3. Re: Checking if an object exists on a page
              Peter Kahrel Adobe Community Professional & MVP

              The function you use to check if a frame exists has an error: with(myDocument.pages.everyItem){ should be with(myDocument.pages.everyItem()){


              But that function is unnecessary, no need to cycly through all frames on a page. You can do this:

              if (!myDocument.pages.item(0).textFrames.item('myAddressWindow').isValid){  
                  var myAddressWindow=myDocument.pages.item(0).textFrames.add();  
                  var myCorners=CornerOptions.ROUNDED_CORNER;  
                  myAddressWindow.properties={name:"myAddressWindow",geometricBounds:["33mm", "20mm", "78mm", "110mm"],strokeWeight:0.5,strokeColor:"Magenta",topLeftCornerOption:myCorners,topRightCornerOption:myCorners,bottomLeftCornerOption:myCorners,bottomRightCornerOption:myCorners};  
              }
              

               

              Peter

              • 4. Re: Checking if an object exists on a page
                smoothsea Level 3

                Thanks Uwe - that works well! I think I was confusing myself because I was confusing the Javascript object name with the Indesign object name.

                 

                Thanks also to you Peter - both solutions work well, but I would prefer to keep it as a function so that it can be reused to check for any object at any time.

                 

                Regards,

                 

                Malcolm

                • 5. Re: Checking if an object exists on a page
                  Peter Kahrel Adobe Community Professional & MVP

                  Malcolm -- With all due respect, you function makes no sense at all. You want to check if a frame occurs on a page, but the function doesObjectExist() inspects -- or so it seems -- all pages. Furthermore, 'name' in the function doesn't refer to anything.

                   

                  If you want to check any object on a given page rather than just text frames, do

                   

                  if (!myDocument.pages.item(0).pageItems.item(. . .)

                   

                  Peter

                  • 6. Re: Checking if an object exists on a page
                    smoothsea Level 3

                    Ha ha no worries Peter - I'm here to learn after all...

                     

                    Here is the script in its entirety - it's working except for when I try to lock the layer on line 54.

                     

                    var myDocument=app.documents.item(0);
                    
                    
                    // set bleed and slug options
                    
                    
                    with(myDocument.documentPreferences){
                        documentBleedTopOffset="3mm";
                        documentBleedUniformSize=true;
                        slugTopOffset=0;
                        documentSlugUniformSize=true;
                        }
                    
                    
                    removeEmptyLayers();
                    
                    
                    // add position guide layer (if it doesn't already exist)
                    try {
                        myPositionLayer=myDocument.layers.add({name:"WMPosition Guide",printable:false});
                    }
                    catch(myError){
                       
                      };
                    
                    
                    // add fold lines
                    var foldLineWeight=0.5;
                    var foldLineColor="Magenta";
                    var foldLineType="Dashed";
                    
                    
                    // add first fold
                    if(!doesObjectExist("myFirstFold")){
                    var myFirstFold=myDocument.pages.item(0).graphicLines.add();
                    myFirstFold.properties={name:"myFirstFold",geometricBounds:["98mm", "-3mm", "98mm", "213mm"],strokeWeight:foldLineWeight,strokeColor:foldLineColor,strokeType:foldLineType};
                    }
                    
                    
                    if(!doesObjectExist("mySecondFold")){
                    var mySecondFold=myDocument.pages.item(0).graphicLines.add();
                    mySecondFold.properties={name:"mySecondFold",geometricBounds:["198mm", "-3mm", "198mm", "213mm"],strokeWeight:foldLineWeight,strokeColor:foldLineColor,strokeType:foldLineType};
                    }
                    
                    
                    // add address window
                    if(!doesObjectExist("myAddressWindow")){
                        var myAddressWindow=myDocument.pages.item(0).textFrames.add();
                        var myCorners=CornerOptions.ROUNDED_CORNER;
                        // geometricBounds order: top left y,top left x, bottom right y, bottom right x
                        myAddressWindow.properties={name:"myAddressWindow",geometricBounds:["33mm", "20mm", "78mm", "110mm"],strokeWeight:0.5,strokeColor:"Magenta",topLeftCornerOption:myCorners,topRightCornerOption:myCorners,bottomLeftCornerOption:myCorners,bottomRightCornerOption:myCorners};
                    }
                    // lock position guide layer
                    myDocument.layers.item(myPositionLayer).properties={locked:true};
                    
                    
                    function removeEmptyLayers(){
                        var layers=myDocument.layers.everyItem().getElements();
                        for(var i=layers.length-1;i>0;i--){
                            if(layers[i].pageItems.length==0){
                                layers[i].remove();
                                }
                            }
                        }
                    
                    
                    function doesObjectExist(objectName){
                        if(app.documents[0].pageItems.itemByName(objectName).isValid){
                            return true;
                            }
                    
                    
                    }
                    

                     

                     

                    The reason that I want to use a function to check for the existence of an object is that I may want to use it again and again for different objects. I like the idea of reusable chunks of code like that.

                     

                    Regards,

                     

                    Malcolm