9 Replies Latest reply on Jan 18, 2017 2:51 AM by foltman

    active page vs selected page

    foltman Level 1

      Hello to everyone!

       

      I'm trying to find a way to get a reference to a page that is selected in Pages panel but not necessary is active one.

      If I understand this correctly in the below example pages 2-3 are active even though page 4 is selected.

       

      app.activeWindow.activePage - command will give reference to page number 2.

      But what with page number 4? I can't find any method.

       

      screen_pages.png

       

      Thanks in advance,

      jf

        • 1. Re: active page vs selected page
          Harbs. Level 6

          Panel interaction is perhaps the weakest area in InDesign scripting. I don't think there's a way to get the selected pages in the Pages Panel.

          1 person found this helpful
          • 2. Re: active page vs selected page
            foltman Level 1

            Phew!

            Thanks, I thought so.

            I'll try to find a way around it.

            • 3. Re: active page vs selected page
              Laubender Adobe Community Professional & MVP

              Hi Jarek,

              I see a good chance for InDesign CS6 only where you can get the selected pages in the Pages panel.

               

              For all other versions of InDesign—before and after CS6—there is a way to get the selected pages, but it comes with some severe problems. See the list below where I discuss this.

               

              The solution is revolving around using menu commands with the Pages panel.

               

              InDesign CS6 only:

              Menu command with the key string:

              "$ID/kOptionalPageMenuItem".

              In my German version of CS6, this menu command is named:

              "Optional in Liquid-HTML5".

               

              What does "Optional in Liquid-HTML5" do?

              It applies true or false to the page property optionalPage.

              optionalPage was part of an experimental feature with InDesign's EPUB export function (as far as I remember) and is not implemented with all other versions of InDesign but CS6.

               

              How could we use this feature?

              You can check the value of optionalPage on every page and master page ( not the "None" master ) in the document before using the menu command and after using it. Therfore you can retrieve the selected pages in the Pages panel. If master "None" is selected one cannot use the menu command. We could detect this case by using try/catch for instance.

               

              All other versions of InDesign:

              For all other versions—where unfortunately "$ID/kOptionalPageMenuItem" is not available—one of the following three instances of the same menu command can be used. Depending on what exactly is selected.

               

              "$ID/kDuplicatePageCmd" (German: "Seite duplizieren")

              "$ID/kDuplicatePagesCmd" (German: "Seiten duplizieren")

              "$ID/kDuplicateSpreadCmd" (German: "Druckbogen duplizieren")

               

              There are other menu command strings available if you select master pages or all pages on a master.
              Or one could use the menu command for removing pages. I did not fully test all the options available.

               

              Problems are:

              1. You have to undo what you did with the menu command.

              2. If you undo the duplicating of the document pages:

              2.1 The selection in the Pages panel is lost.

              2.2 The active page will be a different one.

              3. I had trouble—means was not able, not tested all ideas—to find the relevant locale-agnostic keystrings if all the pages of a master spread are selected.

               

              No save bet:

              Using the index of the menu commands to execute the associatedMenuAction.

              3rd-party scripts or plug-in could have installed other menu commands in the list before the one you are trying to use.

               

              Regards,
              Uwe

              • 4. Re: active page vs selected page
                Laubender Adobe Community Professional & MVP

                Here some code for InDesign CS6 only where I exploit property optionalPage of the Page object:

                 

                /**
                * @@@BUILDINFO@@@ GetPages-SELECTED-IN-PAGES-PANEL-v1.0.jsx !Version! Fri Jan 13 2017 11:41:41 GMT+0100
                */
                
                /*
                    Script published at Adobe InDesign Scripting forum:
                   
                    active page vs selected page
                    Jarek Foltman Jan 10, 2017
                    https://forums.adobe.com/message/9251733#9251733
                   
                */
                
                // CS6 ONLY !!
                // Example usage:
                
                app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
                
                app.doScript
                    (
                   
                    addTextFrameToSelectedPagesInThePagesPanel,
                    ScriptLanguage.JAVASCRIPT,
                    [],
                    UndoModes.ENTIRE_SCRIPT,
                    "Add text frame to the selected pages in the Pages panel | SCRIPT"
                   
                    );
                
                // USE CASE EXAMPLE:
                
                function addTextFrameToSelectedPagesInThePagesPanel()
                {
                
                    if(parseInt(app.version) != 8)
                    {
                        alert
                        (
                            "SORRY:"+"\r"+
                            "This is script can ONLY work with InDesign CS6."
                        );
                        return
                    }
                   
                    var doc = app.documents[0];
                    var selectedPagesArray = getSelectedPagesInPagesPanel();
                
                    for(var n=0;n<selectedPagesArray.length;n++)
                    {
                        var textFrame = selectedPagesArray[n].textFrames.add
                        (
                            {
                                geometricBounds : selectedPagesArray[n].bounds ,
                                fillColor : doc.colors.itemByName("Yellow") ,
                                strokeColor : "None" ,
                                contents : "Selected in Pages panel."
                            }
                        )
                        textFrame.texts[0].pointSize = 80;
                        textFrame.texts[0].justification = Justification.CENTER_ALIGN;
                        textFrame.textFramePreferences.verticalJustification = VerticalJustification.CENTER_ALIGN;
                    }
                
                };
                
                // Will return empty array or array with pages
                // Could be pages of master spreads or document pages
                
                function getSelectedPagesInPagesPanel()
                {
                    var resultArray = [];
                   
                    if(app.documents.length == 0){return resultArray };
                   
                    // What happens, if the Pages panel is closed?
                    // Is the selection of pages still valid?
                   
                    // RESULT AFTER TESTING:
                    // If the Pages panel is closed, after opening it again, the first page of the document is always selected.
                    // So it's best I think to do nothing if the Pages panel is closed.
                    // Return empty array:
                   
                    if(!app.panels.itemByName("$ID/Pages").visible){return resultArray };
                   
                    var doc = app.documents[0];
                    var oldOptionalPages = app.documents[0].pages.everyItem().optionalPage;
                    var oldOptionalMasterPages = app.documents[0].masterSpreads.everyItem().pages.everyItem().optionalPage;
                
                    var docPages = doc.pages.everyItem().getElements();
                    var masterPages = doc.masterSpreads.everyItem().pages.everyItem().getElements();
                
                    doc.pages.everyItem().optionalPage = false;
                    doc.masterSpreads.everyItem().pages.everyItem().optionalPage = false;
                
                    try
                    {
                    app.menus.item("$ID/PagesPanelPopup").
                        submenus.itemByName("$ID/PageAttributesSubMenu").
                        menuItems.itemByName("$ID/kOptionalPageMenuItem").
                        associatedMenuAction.invoke();
                    }catch(e)
                    {
                        // Master "None" selected in the Pages Panel
                        return resultArray;
                    }
                       
                    var optionalPages = app.documents[0].pages.everyItem().optionalPage;
                    var optionalPagesLength = optionalPages.length;
                
                    var optionalMasterPages = app.documents[0].masterSpreads.everyItem().pages.everyItem().optionalPage;
                    var optionalMasterPagesLength = optionalMasterPages.length;
                
                    for(var n=0;n<optionalPagesLength;n++)
                    {
                        if(optionalPages[n] == true)
                        {
                            resultArray[resultArray.length++] = docPages[n];
                        };
                        // Reset the old value:
                        docPages[n].optionalPage = oldOptionalPages[n];
                    };
                
                    for(var n=0;n<optionalMasterPagesLength;n++)
                    {
                        if(optionalMasterPages[n] == true)
                        {
                            resultArray[resultArray.length++] = masterPages[n];
                        };
                        // Reset the old value:
                        masterPages[n].optionalPage = oldOptionalMasterPages[n];
                    };
                   
                    return resultArray;
                
                };
                

                 

                Some screenshots:

                 

                Case A, document pages selected in the Pages panel.

                Before running the script:

                 

                1-DocumentPagesSelected.png

                 

                After running the script:

                 

                2-DocumentPagesSelected-AfterScriptRun.png

                 

                 

                Case B, master pages selected in the Pages panel.

                Before running the script:

                 

                3-MasterPagesSelected.png

                 

                After running the script:

                 

                4-MasterPagesSelected-AfterScriptRun.png

                 

                Regards,
                Uwe

                • 5. Re: active page vs selected page
                  Laubender Adobe Community Professional & MVP

                  Here another idea for none-CS6 versions above CS5 (edit: AND also for CS6):

                  We could try to reach and execute the menu command for page labels and compare the state of labels before and after invoking the menu. That possibly would not affect the selection state of the selected pages in the Pages panel.

                   

                  Regards,
                  Uwe

                   

                  //EDITED

                  • 6. Re: active page vs selected page
                    Laubender Adobe Community Professional & MVP

                    Hi together,

                     

                    below some code that is looking for active pages in the Pages panel where the property pageColor is exploited.

                    Since pageColor cannot be applied individuallly on pages on a given masterSpread—all pages on a master spread are colored the same, if you apply pageColor to a single page, one cannot detect if a single page on a master is selected in the Pages panel.

                     

                    However, for selected document pages the function is working as it should.

                     

                    Notes and some important issues:

                    1. The code below is overwriting already set values of pageColor. It's up to you to refine it.

                    2. In case of selected master pages, one cannot tell the exact page, only the spread.

                    3. If the "None" master is selected, it will return an empty array.

                    4. It's for CS6 and above, because the menu structure changed from CS5.5 to CS6.

                    But it would be easy to do a version for CS5 and CS5.5 as well, I think.

                     

                    Tested with CC 2017 on Mac OSX 10.10.5, but should also run on Windows.

                     

                    /**
                    * @@@BUILDINFO@@@ GetPages-SELECTED-IN-PAGES-PANEL-v2.0.jsx !Version! Sun Jan 15 2017 14:10:32 GMT+0100
                    */
                    
                    /*
                       
                        1. ISSUE | If a single master page is selected, all pages of the master are stored as selected.
                        2. ISSUE | The script will reset all pages' pageColor to: PageColorOptions.USE_MASTER_COLOR
                        ToDo:
                        Get the initial state of pageColor for all pages and apply it again after retrieving the info.
                        Or use several undos.
                       
                        3. ISSUE | CS6 and above
                        ToDo: Do a seperate version for CS5 and CS5.5.
                       
                    */
                    
                    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
                    
                    app.doScript
                        (
                       
                        addTextFrameToSelectedPagesInThePagesPanel,
                        ScriptLanguage.JAVASCRIPT,
                        [],
                        UndoModes.ENTIRE_SCRIPT,
                        "Add text frame to the selected pages in the Pages panel | SCRIPT"
                       
                        );
                    
                    // USE CASE EXAMPLE:
                    
                    function addTextFrameToSelectedPagesInThePagesPanel()
                    {
                    
                        if(parseInt(app.version) < 8)
                        {
                            alert
                            (
                                "SORRY:"+"\r"+
                                "This is script can ONLY work with InDesign CS6 and later."
                            );
                            return
                        }
                       
                        var doc = app.documents[0];
                        var selectedPagesArray = getSelectedPagesInPagesPanel();
                    
                        for(var n=0;n<selectedPagesArray.length;n++)
                        {
                            var textFrame = selectedPagesArray[n].textFrames.add
                            (
                                {
                                    geometricBounds : selectedPagesArray[n].bounds ,
                                    fillColor : doc.colors.itemByName("Yellow") ,
                                    strokeColor : "None" ,
                                    contents : "Selected in Pages panel."
                                }
                            )
                            textFrame.texts[0].pointSize = 80;
                            textFrame.texts[0].justification = Justification.CENTER_ALIGN;
                            textFrame.textFramePreferences.verticalJustification = VerticalJustification.CENTER_ALIGN;
                        }
                    
                    };
                    
                    // Will return empty array or array with pages
                    // Could be pages of master spreads or document pages
                    
                    function getSelectedPagesInPagesPanel()
                    {
                        var resultArray = [];
                       
                        if(app.documents.length == 0){return resultArray };
                       
                        // What happens, if the Pages panel is closed?
                        // Is the selection of pages still valid?
                       
                        // RESULT AFTER TESTING:
                        // If the Pages panel is closed, after opening it again, the first page of the document is always selected.
                        // So it's best I think to do nothing if the Pages panel is closed.
                        // Return empty array:
                       
                        if(!app.panels.itemByName("$ID/Pages").visible){return resultArray };
                       
                        var doc = app.documents[0];
                       
                        doc.masterSpreads.everyItem().pages.everyItem().pageColor = PageColorOptions.USE_MASTER_COLOR;
                        doc.pages.everyItem().pageColor = PageColorOptions.USE_MASTER_COLOR;
                    
                        try
                        {
                        app.menus.item("$ID/PagesPanelPopup").
                            submenus.itemByName("$ID/PageAttributesSubMenu").
                            submenus.itemByName("$ID/PageColorLabelMenu").
                            menuItems[-1].associatedMenuAction.invoke();
                        }catch(e)
                        {
                            // Master "None" selected in the Pages Panel
                            return resultArray;
                        }
                           
                        var masterPages = doc.masterSpreads.everyItem().pages.everyItem().getElements();
                        var masterPagesLength = masterPages.length;
                        var docPages = doc.pages.everyItem().getElements();
                        var docPagesLength = docPages.length;
                    
                        // Master pages first:
                    
                        for(var n=0;n<masterPagesLength;n++)
                        {
                            if(masterPages[n].pageColor != 1346594413)
                            {
                                resultArray[resultArray.length++] = masterPages[n];
                            };
                       
                            // $.writeln(n+"\t"+"master | pageColor"+"\t"+masterPages[n].pageColor.toString() );
                        };
                    
                        // Then document pages:
                       
                        for(var n=0;n<docPagesLength;n++)
                        {
                            if(docPages[n].pageColor != 1346594413)
                            {
                                resultArray[resultArray.length++] = docPages[n];
                            };
                       
                            // $.writeln(n+"\t"+"doc | pageColor"+"\t"+docPages[n].pageColor.toString() );
                       
                        };
                       
                        doc.masterSpreads.everyItem().pages.everyItem().pageColor = PageColorOptions.USE_MASTER_COLOR;
                        doc.pages.everyItem().pageColor = PageColorOptions.USE_MASTER_COLOR;
                       
                        return resultArray;
                    
                    };
                    

                     

                    Regards,
                    Uwe

                    • 7. Re: active page vs selected page
                      foltman Level 1

                      Wow, Uwe, that one is great! It is exactly what I was looking for.

                      I'm digging into it right now, making modifications needed for my purpose.

                      I would newer thought about exploring color label properties.

                       

                      Thank you very much!

                      Jarek

                      • 8. Re: active page vs selected page
                        Laubender Adobe Community Professional & MVP

                        Hi Jarek,

                        the one for InDesign CS6 only is more insteresting, because it can detect also selected master pages, but unfortunately the menu I exploited for that is not available anymore in higher versions.

                         

                        Cheers,

                        Uwe

                        • 9. Re: active page vs selected page
                          foltman Level 1

                          Hello Uwe!

                           

                          I've noticed that in cc you can only apply label to whole master spread, not to single page. But even with this limitation your method works very well.

                          So once again thanks for it.

                           

                          Best,

                          Jarek