17 Replies Latest reply on May 17, 2016 2:38 AM by bhumikas35064042

    PageItem showing as valid when checked with JS but it actually does not exists in the document.

    bhumikas35064042

      Hello All,

       

      There is a pageItem with id "1784" which when checked with the help of JS

       

      var theDoc = app.documents.item(0);

      var thePItem = theDoc.pageItems.itemByID(1784);

      $.writeln(thePItem.isValid);

       

      comes out to be valid but in real this pageItem does not exists in the document..

       

      There by when checking for properties like parent of that pageItem the script crashes...


      Can anyone help me out with this as to how can we check the validity of a pageItem in this case and how can this pageItem turn out to be valid if it does not actually exists in the document...

       

      Link includes the document file ...

      https://www.dropbox.com/s/do2449rgdlc61qa/testData.indd?dl=0

       

      Any help would be appreciated ..

       

      Thanks in advance

      Bhumika

        • 1. Re: PageItem showing as valid when checked with JS but it actually does not exists in the document.
          Laubender Adobe Community Professional & MVP

          Hi Bhumika,

          iterate through the allPageItems array of the document and look for IDs.

          A pageItem with the id 1784 is not there.

           

          It is a misconception to think all available IDs for the PageItem Class denote an object on the page.

          What will this return where the pageItem is resolved? :

           

          app.documents[0].pageItems.itemByID(1).getElements()[0];
          // Returns: [object Document]
          

           

          What will this return in your case?

           

          app.documents[0].pageItems.itemByID(1784).getElements()[0];
          // Returns: [object PageItem]
          

           

          I ran the following script on your document from my German ESTK:

           

          var doc = app.documents[0];
          
          for(var n=1;n<2000;n++)
          {
              try{
              $.writeln(n+"\t"+doc.pageItems.itemByID(n).getElements()[0]);
              }catch(e){n+"\t"+e.message};
          }
          

           

          Here the results.

          Note, that some IDs are missing without being caught by the error handler:

           

          1    [object Document]
          7    [object Ink]
          8    [object Ink]
          9    [object Ink]
          10    [object Ink]
          11    [object Color]
          12    [object Color]
          13    [object Color]
          14    [object Swatch]
          15    [object Color]
          16    [object Color]
          17    [object Color]
          55    [object NumberingList]
          57    [object Language]
          78    [object Language]
          79    [object Language]
          110    [object CharacterStyle]
          111    [object ParagraphStyle]
          114    [object ParagraphStyle]
          117    [object CellStyle]
          119    [object TableStyle]
          121    [object TableStyle]
          124    [object Color]
          125    [object Gradient]
          126    [object Color]
          127    [object NamedGrid]
          128    [object ObjectStyle]
          130    [object ObjectStyle]
          131    [object ObjectStyle]
          132    [object ObjectStyle]
          134    [object TOCStyle]
          135    [object CompositeFont]
          136    [object CompositeFontEntry]
          138    [object CompositeFontEntry]
          139    [object CompositeFontEntry]
          140    [object CompositeFontEntry]
          141    [object CompositeFontEntry]
          143    [object CompositeFontEntry]
          144    [object XmlStory]
          169    [object Layer]
          170    [object TrapPreset]
          171    [object TrapPreset]
          172    [object Section]
          175    [object XMLTag]
          177    [object MasterSpread]
          182    [object Page]
          191    [object Assignment]
          194    [object Color]
          195    [object Color]
          196    [object Color]
          197    [object Color]
          198    [object Color]
          199    [object Ink]
          200    [object Color]
          201    [object Color]
          202    [object Color]
          203    [object Color]
          204    [object Color]
          205    [object Color]
          206    [object Color]
          207    [object Color]
          209    [object Gradient]
          210    [object Gradient]
          211    [object Gradient]
          212    [object Gradient]
          218    [object CharacterStyle]
          219    [object ParagraphStyle]
          220    [object ParagraphStyle]
          221    [object ParagraphStyle]
          257    [object Layer]
          262    [object Spread]
          275    [object Page]
          280    [object Guide]
          281    [object Guide]
          282    [object Guide]
          283    [object Guide]
          284    [object Guide]
          285    [object Guide]
          286    [object Guide]
          287    [object Guide]
          288    [object Guide]
          289    [object Guide]
          1027    [object Group]
          1028    [object GraphicLine]
          1029    [object GraphicLine]
          1030    [object GraphicLine]
          1031    [object GraphicLine]
          1032    [object GraphicLine]
          1034    [object Story]
          1052    [object TextFrame]
          1055    [object GraphicLine]
          1056    [object GraphicLine]
          1057    [object GraphicLine]
          1058    [object GraphicLine]
          1059    [object GraphicLine]
          1061    [object Story]
          1079    [object TextFrame]
          1082    [object GraphicLine]
          1083    [object GraphicLine]
          1084    [object GraphicLine]
          1086    [object Story]
          1104    [object TextFrame]
          1107    [object GraphicLine]
          1108    [object GraphicLine]
          1109    [object GraphicLine]
          1111    [object Story]
          1129    [object TextFrame]
          1132    [object GraphicLine]
          1134    [object Story]
          1152    [object TextFrame]
          1668    [object CrossReferenceFormat]
          1669    [object CrossReferenceFormat]
          1670    [object CrossReferenceFormat]
          1671    [object CrossReferenceFormat]
          1672    [object CrossReferenceFormat]
          1673    [object CrossReferenceFormat]
          1674    [object CrossReferenceFormat]
          1675    [object CrossReferenceFormat]
          1676    [object CrossReferenceFormat]
          1780    [object PageItem]
          1781    [object PageItem]
          1782    [object PageItem]
          1783    [object PageItem]
          1784    [object PageItem]
          1826    [object Color]
          1827    [object Color]
          1836    [object XMLTag]
          1837    [object XMLTag]
          1841    [object XMLTag]
          1999    Objekt ist ungültig
          

           

          "Objekt ist ungültig" => "Object is not valid" ( message from the error handler )

           

          So the pageItem with the ID 1784 is existing, but it is not the item that you think it is.

          And yes, reading out the properties will crash InDesign.

           

          Uwe

           

          Btw.: This thread should be moved to InDesign Scripting

          • 3. Re: PageItem showing as valid when checked with JS but it actually does not exists in the document.
            bhumikas35064042 Level 1

            Thread moved to Indesign Script..The specified item was not found.

            • 4. Re: PageItem showing as valid when checked with JS but it actually does not exists in the document.
              Laubender Adobe Community Professional & MVP

              bhumikas35064042 wrote:

               

              Thread moved to Indesign Script..The specified item was not found.

              What do you exacly mean by that?

               

              Uwe

               

              PS: Thank you Peter for moving the thread to the right forum.

              • 5. Re: PageItem showing as valid when checked with JS but it actually does not exists in the document.
                bhumikas35064042 Level 1

                Have removed that thread where i moved as i saw Peter had already moved it ....

                 

                Thanks for the reply Uwe and thanks Peter for moving the thread to scripting forums...

                 

                Then my next question would be that why cant we see this item on the document .. I tried to run pageItem.select() but didn't saw any item selected... Is there a way out we could know the location of this Item on the UI and its type as well ...

                • 6. Re: PageItem showing as valid when checked with JS but it actually does not exists in the document.
                  Laubender Adobe Community Professional & MVP

                  Bhumika,

                  why do you think a "regular" pageItem with the ID 1784 is in the document?


                  What do you expect, that pageItem with ID 1784 is?

                  A text frame, a spline item?

                   

                  Uwe

                  • 7. Re: PageItem showing as valid when checked with JS but it actually does not exists in the document.
                    bhumikas35064042 Level 1

                    The flow is that our user selects a pageItem that could be a textFrame, Rectangle, Graphic line, Group or a polygon and can assign a rule to color it or delete it through our plugin..Now, this rule is assigned for this pageItem id and i m not able to locate it ...

                    • 8. Re: PageItem showing as valid when checked with JS but it actually does not exists in the document.
                      Laubender Adobe Community Professional & MVP

                      Hm. Just a guess:

                      Was the supplied document changed by opening it through the Simple-Saveback Service Adobe is providing to downsave a document from a higher version of InDesign? If yes, the IDs of the pageItems would not persist. If storing IDs is what your plugIn does and expects the same IDs on the same pageItems after the document is opened sometimes again. That workflow would fail, if the document is exchanged, opened and saved on a different machine.

                       

                      As I can see from the document history, the initial document was opened by "InDesign Interchange" (IDML), was worked on with InDesign v8.1.0 and was finally saved under v8.0.0 .

                       

                      Uwe

                      • 9. Re: PageItem showing as valid when checked with JS but it actually does not exists in the document.
                        Peter Kahrel Adobe Community Professional & MVP

                        This script:

                         

                        p = app.documents[0].allPageItems;
                        for (i = p.length-1; i >= 0; i--) {
                            $.writeln (p[i].id);
                        }
                        

                         

                        Produces this list:

                         

                        1028

                        1029

                        1030

                        1031

                        1032

                        1052

                        1055

                        1056

                        1057

                        1058

                        1059

                        1079

                        1082

                        1083

                        1084

                        1104

                        1107

                        1108

                        1109

                        1129

                        1132

                        1152

                        1027

                         

                        and these are the IDs of the 'real' page items on your page.

                         

                        But it looks as if one of your plug-ins (naehas) does funny things with your document. Save your document as IDML, unzip it, and open designmap.xml. You'll find various references to 1784, e.g. this one:

                         

                        <KeyValuePair Key="com.naehas.ruleAddedItems" Value="1498,1520,1542,. . .1784,. . ." />

                         

                        That the plug-in does really strange (and unacceptable, I would say) things can be shown by some of Uwe's output:

                         

                        doc.pageItems.itemByID(1670)  >> PageItem

                        doc.pageItems.itemByID(1670).getElements()[0] >> CrossReferenceFormat

                         

                        Is that plug-in scriptable, by the way?

                         

                        Peter

                        • 11. Re: PageItem showing as valid when checked with JS but it actually does not exists in the document.
                          Laubender Adobe Community Professional & MVP

                          Hi Peter,

                          all the KeyValuePair entries from the designmap.xml of the exported IDML can be extracted with doc.extractLabel().

                          Let's do that with the provided InDesign document testData.indd :

                           

                          app.documents[0].extractLabel("com.naehas.ruleAddedItems");
                          /*
                          
                          1498,1520,1542,1564,1586,1608,1630,1652,1674,1696,1718,1740,1762,1784, … ,4074,4869
                          
                          */
                          

                           

                          If the values once corresponded with IDs of items on the page, they all are not valid anymore with the provided document.

                          My assumption is, that the document was closed, opened with a version of InDesign, that does not has the plug-in installed, that pageItems were removed and/or the IDs changed after opened again with an IDML representation of the document.

                           

                          The strategy of the plug-in storing pageItems by their ID is failing then.

                          This is a failure of conception.

                           

                          If the values do not correspond with the IDs of pageItems, I cannot see what they are good for.

                          At least the test document does not provide the number of pageItems that "com.naehas.ruleAddedItems" is storing as IDs. If corresponding to real IDs or not.

                           

                          Btw. It is not unusual, that a distinct ID of a pageItem corresponds with a CrossReferenceFormat.
                          Not at all, if you loop against the numbers of IDs starting from 1 as I did with my snippet.

                          As we can show the ID number 1 always corresponds with the document itself.

                           

                          Colors are counting, stories and so on, if we use pageItems.itemByID(n) in the loop.

                          Just add a new, empty document and loop the ID numbers from 1 to e.g. 300:

                           

                          var doc = app.documents.add();
                          
                          for(var n=1;n<300;n++)
                          {
                              try{
                              $.writeln(n+"\t"+doc.pageItems.itemByID(n).getElements()[0]);
                              }catch(e){n+"\t"+e.message};
                          }
                          
                          /*
                          
                          Results:
                          
                          1    [object Document]
                          7    [object Ink]
                          8    [object Ink]
                          9    [object Ink]
                          10    [object Ink]
                          11    [object Color]
                          12    [object Color]
                          13    [object Color]
                          14    [object Swatch]
                          15    [object Color]
                          16    [object Color]
                          17    [object Color]
                          18    [object Color]
                          19    [object Color]
                          20    [object Color]
                          21    [object Color]
                          22    [object Color]
                          23    [object Color]
                          61    [object NumberingList]
                          63    [object Language]
                          64    [object Language]
                          65    [object Language]
                          66    [object Language]
                          67    [object Language]
                          68    [object Language]
                          69    [object Language]
                          70    [object Language]
                          71    [object Language]
                          72    [object Language]
                          73    [object Language]
                          74    [object Language]
                          75    [object Language]
                          76    [object Language]
                          77    [object Language]
                          78    [object Language]
                          79    [object Language]
                          80    [object Language]
                          81    [object Language]
                          82    [object Language]
                          83    [object Language]
                          84    [object Language]
                          85    [object Language]
                          86    [object Language]
                          87    [object Language]
                          88    [object Language]
                          89    [object Language]
                          90    [object Language]
                          91    [object Language]
                          92    [object Language]
                          93    [object Language]
                          94    [object Language]
                          95    [object Language]
                          96    [object Language]
                          97    [object Language]
                          98    [object Language]
                          99    [object Language]
                          100    [object Language]
                          101    [object Language]
                          102    [object Language]
                          103    [object Language]
                          104    [object Language]
                          105    [object Language]
                          106    [object Language]
                          107    [object Language]
                          108    [object Language]
                          109    [object Language]
                          110    [object Language]
                          111    [object Language]
                          112    [object Language]
                          113    [object Language]
                          114    [object Language]
                          115    [object Language]
                          116    [object CharacterStyle]
                          117    [object ParagraphStyle]
                          120    [object ParagraphStyle]
                          133    [object CellStyle]
                          135    [object TableStyle]
                          137    [object TableStyle]
                          139    [object Color]
                          140    [object Gradient]
                          141    [object Color]
                          143    [object NamedGrid]
                          144    [object ObjectStyle]
                          146    [object ObjectStyle]
                          147    [object ObjectStyle]
                          148    [object ObjectStyle]
                          150    [object CrossReferenceFormat]
                          151    [object CrossReferenceFormat]
                          152    [object CrossReferenceFormat]
                          153    [object CrossReferenceFormat]
                          154    [object CrossReferenceFormat]
                          155    [object CrossReferenceFormat]
                          156    [object CrossReferenceFormat]
                          157    [object CrossReferenceFormat]
                          158    [object CrossReferenceFormat]
                          159    [object TOCStyle]
                          160    [object CompositeFont]
                          161    [object CompositeFontEntry]
                          163    [object CompositeFontEntry]
                          164    [object CompositeFontEntry]
                          165    [object CompositeFontEntry]
                          166    [object CompositeFontEntry]
                          167    [object CompositeFontEntry]
                          168    [object XmlStory]
                          185    [object XMLTag]
                          194    [object Layer]
                          195    [object Layer]
                          196    [object TrapPreset]
                          197    [object TrapPreset]
                          198    [object Spread]
                          203    [object Page]
                          204    [object Section]
                          205    [object MasterSpread]
                          210    [object Page]
                          211    [object Page]
                          212    [object Assignment]
                          213    [object ScriptedPlugin]
                          Result: 299    Objekt ist ungültig
                          
                          */
                          

                           

                          Depending on the version of InDesign, installed plug-ins, the list would be a bit different.

                          I ran the snippet above on InDesign CS6 v8.1.0 on Mac OSX 10.7.5.

                          ( But this all is a bit academical… )

                           

                          Uwe

                          • 12. Re: PageItem showing as valid when checked with JS but it actually does not exists in the document.
                            Laubender Adobe Community Professional & MVP

                            Bhumika,

                            if you test a distinct ID with pageItems.itembyID(n) I strongly suggest you should always resolve the pageItem and see, if it is not a pageItem anymore.

                             

                            var id = 1784;
                            var doc = app.documents[0];
                            
                            var resolvedObject = doc.pageItems.itemByID(id).getElements()[0];
                            
                            if( resolvedObject.constructor.name == "PageItem" )
                            {
                                alert("PageItem could not be resolved sufficiently. Whatever this is, it's not on your spread.")
                            };
                            
                            else
                            {
                                 /*do something with that item on the page*/
                            };
                            

                             

                            Regards,
                            Uwe

                            • 13. Re: PageItem showing as valid when checked with JS but it actually does not exists in the document.
                              bhumikas35064042 Level 1

                              Hi Uwe,

                               

                              Thanks for providing with the way out to resolve pageItems before i could actually proceed with the further properties check which were crashing my code......

                               

                              Have asked my client about the assumption you made.... Will get back to you with their reply when ever i will get.....
                              For the time being they have told me to remove the rules for such pageItem that actually exists but are not visible on the document so the implementation worked for them after that.......

                               

                              Thanks

                              Bhumika

                              • 14. Re: PageItem showing as valid when checked with JS but it actually does not exists in the document.
                                Loic.Aigon Adobe Community Professional

                                Hi,

                                 

                                FWIW, Flex based panels are really counting down their days left (each new CC version may result in swf panels support drop). So unless you are specifically targeting CS5-CS6 apps, you are taking a bit of a risk regarding your developments efforts.

                                 

                                Just saying,

                                 

                                Loic

                                • 15. Re: PageItem showing as valid when checked with JS but it actually does not exists in the document.
                                  bhumikas35064042 Level 1

                                  Hi Uwe,

                                   

                                  Yes you are correct on your assumptions as per the confirmation from my Client...The files were received in CC and converted to CS6 with an intermediate step of converting to IDML.

                                  • 16. Re: PageItem showing as valid when checked with JS but it actually does not exists in the document.
                                    Laubender Adobe Community Professional & MVP

                                    So, I think this question is answered.

                                     

                                    What is not answered is what you can do about it, if a user of your plug-in is stepping out of the system and adds, removes, duplicates page items or uses IDML to process the document elsewhere and is coming back to the machine where your plug-in or extension is installed.

                                     

                                    I have no answer for this, since e.g. using labels on page items for tracking would fail, if page items would be duplicated (the labels would be duplicated as well).

                                     

                                    Uwe

                                    • 17. Re: PageItem showing as valid when checked with JS but it actually does not exists in the document.
                                      bhumikas35064042 Level 1

                                      Yes my question is answered ..... Thanks alot for your help.....