1 Reply Latest reply on Feb 2, 2017 7:13 AM by manibenna

    Adding section automatically when pages are added (javascript)

    manibenna

      Hi, when a user adds one or more pages, i need to automatically start a new section (with page number 1) on each my page that has certain features, for example if a master page with a certain name is applied to my page.

      I don't know if we can manage it by events, does anyone know how to do?

        • 1. Re: Adding section automatically when pages are added (javascript)
          manibenna Level 1

          For now, I solved in this way:

           

          //startup script 
          #targetengine "session" 
          main();
          function main(){
              var myAfterOpen = app.eventListeners.add("afterOpen", watchDoc); //I listen for the "afterOpen" event, but I don't know the exact object that fires this event
          } 
          
          
          function watchDoc(myEvent){ 
              var myObj = myEvent.parent; //The parent of the event is the document.
              if (myObj.constructor.name == "Document") { //If the type of the object that fired the event is "Document"
                  var myDoc = myObj;
                  if (myDoc.label === "captaEventi"){ //I check the lable of the document
                      alert("Per questo documento si attivano automaticamente alcuni script in base a determinati eventi."); //Alert for the user
                      var myAfterAttributeChanged = app.eventListeners.add("afterAttributeChanged", watchPages);  //I listen for an other type of event that fires when the user adds/removes/selects pages
                  }
              }
          }
          function watchPages(myEvent){
              var myObj = myEvent.parent;
              if (myObj.constructor.name == "LayoutWindow") {
                  var myDoc = myObj.parent;
                  var pagesList = myDoc.pages;
                  for (i = 0; i < pagesList.length; i++){ //I analyze each my page of the document
                      var myPage = pagesList[i];
                      if (myPage.appliedMaster != null){ //If a master page is applied to my page
                          var sezioneGiaPresente = myPage.appliedSection; //I get the section to which the page belongs
                          if (myPage.appliedMaster.name !== "N-Distinta NoTab" && sezioneGiaPresente.pageStart.name != myPage.name){//If the master page has a certain name and the section doesn't begin on my page
                              var newSect = myDoc.sections.add(myPage); //I set a new section starting on myPage
                          }
                      }
                  }
              }
          }