8 Replies Latest reply on Apr 19, 2012 6:02 AM by Green4ever

    Count page's with color label

    beh_gras_beh Level 1

      Dear people,

       

      Is it possible that InDesign can count the pages with a color label? (CS5)

       

      So a window appears with the message.

      10 yellow

      20 red

      etc. etc.

       

      I' was searching arround the forum and cannot find anything with colorlabels.

       

      Thanks for anyone who want to help!

       

       

      Greetings from Holland

        • 1. Re: Count page's with color label
          [Jongware] Most Valuable Participant

          (Without testing) Try using the pageColor property of your pages.

          1 person found this helpful
          • 2. Re: Count page's with color label
            beh_gras_beh Level 1

            Thanks I'l try that!

            • 3. Re: Count page's with color label
              Marijan Tompa [tomaxxi] Level 4

              This will do the job:

               

              var
                  mD = app.activeDocument,
                  mP = mD.pages,
                  pC = {}, i = 0;
              
              for ( i ; i < mP.length ; i++ )
                  ! pC [ mP [ i ].pageColor ] ? pC [ mP [ i ].pageColor ] = 1 : pC [ mP [ i ].pageColor ] += 1;
              
              alert ( pC.toSource () );
              

               

              HTH

               

              --

              Marijan (tomaxxi)

              http://tomaxxi.com

              • 4. Re: Count page's with color label
                beh_gras_beh Level 1

                Wow!

                 

                That was fast!

                Was trying but ended with inDesign crashing.

                 

                This is realy good! (and better).

                 

                Greetings from Holland

                • 5. Re: Count page's with color label
                  [Jongware] Most Valuable Participant

                  Good one, Marijan! I was thinking of an associative array as well, but I could not have written it as concise as you did.

                   

                  For the inevitably flabbergasted lurkers:

                   

                  a. You can use arrays like this: element[0], element[1], .. element[n], which is the regular way of addressing individual elements, but you can also address them associative: element['value'], element['name'], element['whatever']. You can use whatever you like as the index. This way, you don't have to remember '0 is for yellow, 1 is for red, 2 is for blue' -- you simply use element['yellow'], element['red'] and element['blue']. The associativity has nothing to do with the actual contents of each array element.

                   

                  b. So Marijan uses 'pageColor' to store the counter for each page color -- if you encounter a page with a color label 'red', he adds 1 to pC['red'].

                   

                  c. Problem is, you cannot add 1 to something that does not exist. If you encounter a page color 'purple' for the first time, you cannot say

                   

                  pC['purple'] = pC['purple']+1;

                   

                  (or a shorthand equivalent of the same) because it doesn't exist yet!

                   

                  d. A non-existing array element is equal to null -- "nothing". (This is not the same as '0' (zero).) Now while you can check for

                   

                  if (pC['purple'] == null) ..

                   

                  you can also use the logical operator ! (not), which tests for equal-to-zero (and in this case, null is treated as if it is 0 -- that actually makes sense because it's certainly not not zero). The line

                   

                  !(object)

                   

                  will result in true if object equals null, false otherwise -- !x tests for zero, so if x is zero, the result is true. Capice?

                   

                  e. By way of encore Marijan packs the entire test-if-exist/create-new/add-one into a single line. The notation

                   

                  a ? b : c;

                   

                  works like this: if (a) is true then do (b) else do (c). So if 'new color' is null -- it doesn't exist -- then add a new associated element (and set its counter value to '1', by the way), else add '1' to the value it already had.

                  • 6. Re: Count page's with color label
                    TᴀW Adobe Community Professional & MVP

                    Very nice Marijan, and thanks for the commentary Jongware!

                     

                    It looks like pC has been declared as an object though, not an array?

                     

                    Also, the toSource() idea is a very nice quick way of showing all the

                    information.

                     

                    Ariel

                    • 7. Re: Count page's with color label
                      [Jongware] Most Valuable Participant

                      Arïel wrote:

                       

                      It looks like pC has been declared as an object though, not an array?

                       

                      Yeah I missed that. Good point.

                       

                      Perhaps it's this: objects behave the same as an array, except for the fact that an array will never show its index. An object always consists of explicit key-value pairs, and toSource automatically shows both. Apparently you can use anything for an array index, but there is no obvious way to "recall" which one were used!

                       

                      test_array = [];
                      test_object = {};
                      test_array['red'] = 'blue';
                      test_object['green'] = 'yellow';
                      alert ('['+test_array['red']+']\r\r['+test_array.toSource()+']\r\r['+test_array.reflect.properties+']\r\r['+test_object.toSource()+']');
                      

                       

                      This shows test_array['red'] is a known item, but you cannot see its value unless you know there is an item called 'red' to begin with! (Unless you use 'reflect.properties', which will then return too much information.)

                      • 8. Re: Count page's with color label
                        Green4ever Level 3

                        Awesome.... code. Simple but powerful.

                         

                        Green4ever