11 Replies Latest reply on Feb 12, 2014 4:06 PM by Timsig

    Script to delete alternate layout

    Timsig Level 1

      I am writing a script that puts ID documents into the correct folder and naming structure to be batch imported into a folio. The documents have two alternate layouts, the original "Print" layout and an "I" layout for ipad. I want the script to delete the "Print" layout pages before processing the document. This is effectively the first section of the document. If I try to target it as below, I receive an error that says cannot delete the doc's default section.

       

       

      var allSections = myDoc.sections;
         var numSections = allSections.length;
         
         if (numSections > 1){
             allSections[0].remove();
             }
      

       

      How do I correctly target this section, and what is the correct syntax for targeting it by it's altenate layout name, "Print"

       

      Many thanks,

      Tim

        • 1. Re: Script to delete alternate layout
          Peter Spier Most Valuable Participant (Moderator)

          Moved to the Scripting forum...

          • 2. Re: Script to delete alternate layout
            Laubender Adobe Community Professional & MVP

            //EDIT: Ah. Peter already moved your question to the right forum while I was working on my answer. Thank you, Peter!

             

            @Tim – this is the wrong forum to ask a scripting question.

             

            There is a dedicated  InDesign Scripting Forum:

            http://forums.adobe.com/community/indesign/indesign_scripting?view=discussions&start=0

             

            But I can answer your question here:

             

            1. Removing a section, does NOT remove its pages

            2. If you remove a section (the first one cannot be removed!) all pages that belong to that section will now belong to a different section: the section that is ordered before the section you removed.

             

            If you want to remove all pages of a section, you first have to find out the index of the first and the indexof the last page of that section and then remove pages in one go with the itemByRange() method.

             

            So let's see. Test these lines of code individually to get a feeling for the values they return:

             

            app.documents[0].sections.length;
            app.documents[0].sections[0].length;
            app.documents[0].sections[0].pageStart.documentOffset;
            

             

            So if the first line returns a number higher than 1, we are ready to go.

             

            The second one will give you the number of pages in that particular section.

             

            The third one will give you the index of the first page that particular section will start (considering all pages in the document, beginning with index value 0 at the first page of your document).

             

             

            So we have all ingredients for removing ALL pages of section 1 of the document:

             

            if(app.documents[0].sections.length > 1){
            
                var pagesLengthOfSectionOne = app.documents[0].sections[0].length;
                var pagesOfSectionOneStartAtIndex = app.documents[0].sections[0].pageStart.documentOffset;
            
                app.documents[0].pages.itemByRange(pagesOfSectionOneStartAtIndex,pagesLengthOfSectionOne-1).remove();
            
                };
            

             

            You'll find excellent DOM documentation here:

             

            http://www.jongware.com/idjshelp.html

             

            Uwe

             

            Message was edited by: Laubender

            1 person found this helpful
            • 3. Re: Script to delete alternate layout
              BobLevine MVP & Adobe Community Professional

              There's a dedicated scripting forum and a dedicated DPS forum. I would try

              the scripting forum first.

               

              BTW, when you get this working, I'd interested in hearing about it.

              • 4. Re: Script to delete alternate layout
                Laubender Adobe Community Professional & MVP

                @Bob – Peter Spier already was so kind to move this thread to the scripting forum.

                 

                And: you could try my snippet to remove all pages of the first section of an InDesign file.
                Don't know if it will work as expected in InDesign CS6 or CC in context of alternate layouts.

                 

                Will try testing it in CS6. Cannot test with InDesign CC…

                 

                Uwe

                • 5. Re: Script to delete alternate layout
                  BobLevine MVP & Adobe Community Professional

                  Thanks, Uwe.

                   

                   

                   

                  I answer via email so it’s not always apparent if something’s been moved.

                  • 6. Re: Script to delete alternate layout
                    Laubender Adobe Community Professional & MVP

                    @Bob – first tests in InDesign CS6 with an very basic alternate layout (three sections: "iPad H", "iPad V", "A4 V") are showing that my script will work as expected.

                     

                    Section 1, "iPad H" will be removed flawlessly.

                     

                    Are there bad side effects?
                    If there are any, more testing is needed to show these…

                     

                    Uwe

                    • 7. Re: Script to delete alternate layout
                      Laubender Adobe Community Professional & MVP

                      @Bob – we can also address a destinct alternate layout by searching all sections for its property "alternateLayout" which value is its name you can see in the Pages Panel.

                       

                      Might be that several sections share the same alternate layout.
                      So, in this case we could test for the "alternateLayout" property of all sections and remove ALL sections that share the same value of "alternateLayout".

                       

                      Don't know, if you had that in mind…

                       

                      Maybe let's see what Tim, the OP, has to say.

                       

                      Uwe

                      • 8. Re: Script to delete alternate layout
                        Timsig Level 1

                        Hello,

                        Thanks Uwe for your very clear explanation, and all for your input. Apologies for posting in the wrong spot. The solution above will work perfectly for the case in point, as the layout I want to remove, "Print", is always the first section in the template for this project. For my own interest, and future projects, I would love to know how to correctly target a section by it's alternate layout name.

                        Many thanks,

                        Tim

                        • 9. Re: Script to delete alternate layout
                          Laubender Adobe Community Professional & MVP

                          @Tim – For a general solution removing ALL PAGES of an alternate layout, we need a more elaborate scripting solution.

                           

                          I was wrong with my statement in  post #7 that we have only to test for the value of "alternateLayout" to get all sections that belong to a specific alternate layout.

                           

                          The value of a specific "alternateLayout" in a document is unique.
                          If more than one section is applied to one alternate layout the value of "alternateLayout" is an empty string.

                           

                          So, basically, what I suggest is:

                           

                          1. You have to loop through all sections and test for the value of "alternateLayout".


                          2. If the name of the alternate layout you want to remove is found store the "pageStart.documentOffset" value (( EDIT removed /EDIT )) in a variable for further use after the loop. Let's call that variable "firstPageIndex".

                           

                          Store the length of the section to a variable we can call "lastPageIndex", add the value of "firstPageIndex" immediately (we cannot assume, that our found alternate layout will always start on page indexed zero).

                           

                          Also use a boolean control. Let's call it "targetHit". Since our alternate layout is found now, we will set it to the value "true".

                           

                          3. When the loop hits the next section and the value of "alternateLayout" is an empty string, you know, that this section also belongs to the found alternate layout. Otherwise the value would not be empty, but a name of a different alternate layout we do not want to remove the pages from.

                           

                          You know that exactly, because you ALSO check the value of our control

                          "targetHit". (Remember, in our example its value is "true" now).

                           

                          4. So, if both conditions are met at the same time, add the sum of "pageStart.documentOffset" and "section length" of the current section to the variable "lastPageIndex".

                           

                          5. In case the value of "alternateLayout" is not an empty string AND not the name of our target AND the control value is "true", we have met another alternate layout and we have to break the loop immediately.

                           

                          After the loop we could use our two variables "firstPageIndex" and "lastPageIndex" to remove all pages that are aggregated under the alternate layout name of our target:

                           

                          myDocument.pages.itemByRange( firstPageIndex, lastPageIndex-1 ).remove();
                          

                           

                           

                          Uwe

                           

                          Message was edited by: Laubender

                           

                          Message was edited by: Laubender

                          • 10. Re: Script to delete alternate layout
                            Laubender Adobe Community Professional & MVP

                            Bob Levine wrote:

                            BTW, when you get this working, I'd interested in hearing about it.

                             

                            @Bob – I wondered why that would be interesting for you.

                            If you are scripter like Tim, one could use a function like this in a more complex script doing also other things.

                             

                            If you just want to remove the pages of an alternate Layout, why not going to the Pages Panel and using the menu command for that?

                             

                            Ok. Maybe I'm thinking in the wrong direction…


                            Maybe you mean NOT removing the pages, but removing the sections?

                             

                            If one is moving pages from document A to document B, InDesign CS6 (and above) is always adding sections and alternate layout names to document B. This could be annoying.

                             

                            To remove NOT the pages, but ALL the sections generated (except the first, that cannot be removed), use the following snippet:

                             

                            if(app.documents[0].sections.length > 1){app.documents[0].sections.itemByRange(1,-1).remove()};
                            

                             

                            It basically means: remove the second section to the last section, if there is more than one section.

                             

                            @Tim: Removing sections does not mean removing pages.

                             

                            Uwe

                            • 11. Re: Script to delete alternate layout
                              Timsig Level 1

                              Thanks again, Uwe. Itried your original snippet, and it works perfectly. I understand the principal of what you explained above, and will try to put it inot practice in a future project. As you say, you could remove the alt. layout manually in individual documents. In this instance, the script automatically prepares a folder of indesign files for batch upload into a folio. It resaves each document into the required folder structure and adds the required "_v" to the filename. I have added the check for / remove alternate layouts part to prevent the script from failing due to the document containing layouts of differing sizes.

                               

                              Once again, many thanks for your help,

                              Tim