12 Replies Latest reply on Jun 24, 2010 7:41 AM by Pontus Uggla

    Reference bug?

    Pontus Uggla Level 1

      I have a very strange problem in a big script.


      I duplicate a rectangle, update the script label and save the label and a reference in a array.


      Then I loop through the array and set the images in the rectangles.


      For some reason it updates the same rectangle twice.


      I went back to where I set the references, looped through them and not only did it say that the reference was correct, now the script started working correct. All I did was selecting the rectangle and alert it. So I removed the alert and it still works the way I want, if I remove the select it starts acting weird again.


      I tried something else to see what happend, and just checking if the rectangle is lost by saving it to a variable works.


      This is very strange to me, can someone explain?


      This is what I added that made it work:


      for(r = 0; r < rectangles.length; r++) {

          var test = rectangles[r]['item'].locked;


        • 1. Re: Reference bug?
          Peter Kahrel Adobe Community Professional & MVP

          You should probably process the rectangles back to front:


          for(r = rectangles.length; r > -1; r--) {
             var test = rectangles[r]['item'].locked;

          (Assuming that "rectangles" is a collection.) That your script works when you select a rectangle but not on more than one rectangle makes sense: processing the collection from beginning to end, you remove a rectangle from the collection, thereby messing up references within the collection. If you work from back to front you don't have that problem. If you select a rectangle there's just one element and then it doesn't matter whether you start at the beginning or the end. Could that be it?



          • 2. Re: Reference bug?
            Pontus Uggla Level 1

            I don´t think you really understood...


            I dont delete anything, and the code that i put there doesn´t do anything, but if i don´t put it there i will have a reference error.


            I doesn´t matter how i loop, just as long as i use the reference before returning my array and using it for the next task.


            If i remove the code i posted there will be an error, but im not using the variable test for anything.

            • 3. Re: Reference bug?
              Peter Kahrel Adobe Community Professional & MVP

              > I don´t think you really understood...


              Always a problem. Maybe someone else will.



              • 4. Re: Reference bug?
                Harbs. Level 6

                FWIW, I did not understand either...



                • 5. Re: Reference bug?
                  Pontus Uggla Level 1

                  I dont really understand either...


                  As i said, the only thing that changes bug to success is if i use the reference before returning the array from the function.


                  Its a very large script, I might have time later to make it small enough for you to download so you can see what i meen.


                  But since my hack is working...

                  • 6. Re: Reference bug?
                    Harbs. Level 6

                    But you haven't really showed us what you are doing.


                    What is: rectangles[r]['item'].locked;?


                    This construction makes no sense to me.



                    • 7. Re: Reference bug?
                      Pontus Uggla Level 1

                      It could have been anything that uses the reference.


                      rectangles is the array that contains objects where item is the rectangle.


                      Looping through rectangles with the variable r as in rectangle.


                      I started with:


                      for(r = 0; r < rectangles.length; r++) {





                      I dont think the variable test was even neccesary, but it worked, as long as i used the reference.


                      Its like Im forcing inDesign to go through the items to remember which on is which.


                      I know its strange, but i will try to come back with a better example.

                      • 8. Re: Reference bug?
                        Harbs. Level 6

                        No. I don't understand your construct at all.


                        I'm assuming tectangles is a collection or an array of rectangles.


                        That would make rectangles[r] a Rectangle. Right?


                        Rectangle['item'] is the same thing as Rectangle.item. A rectangle does not have an "item" property. Acording to how I understand your code, you should be getting an error...



                        • 9. Re: Reference bug?
                          Pontus Uggla Level 1

                          I have 7 pictures that I want to place in a document, but right now i only have 2 rectangles.


                          I ask a function to make sure that i have 7 rectangles with correct label.


                          This function then returns an array with objects containing a reference and the label that contains alot of info parsed in another object like:



                              item: [my rectangle],

                              parsed_label: {

                                  item_id: 71,

                                  date: 2010-01-01 12:21:32,

                                  order_by: 2




                          My item then becomes: rectangles[0]['item]; and the date is rectangles[0]['parsed_label']['date'].


                          Rectangles is then passed back and another function goes through it and checke the item_id, does a query to see what pictures is supposed to be there etc, but it doesn´t update order_by = 4, instead it updateds order_by = 7 twice.


                          This means that i end up with one box empty, and it took a long time to figure out that if i only did a loop as i said it works. Still dont know why.


                          As I sayed, its a long and a bit complex script.

                          • 10. Re: Reference bug?
                            Harbs. Level 6

                            Okay. Now things make a bit more sense.


                            How are you getting your references to your rectangles?



                            • 11. Re: Reference bug?
                              Pontus Uggla Level 1

                              I go through the rectangles of that page and match labels with regex.


                              This is an example of a label:


                              catalog_id: 4;
                              lang_id: 1;
                              master_spread_name: 2x4-Master;
                              master_spread_page: 1;
                              page: 1;
                              item: 1;
                              product_id: 690;
                              created_time: 2010-06-24 16:22:40;
                              last_modified: 2010-06-24 15:22:29;
                              orig_label: mainText;

                              The reason why i put it all in the label instead of using insertLabel() is so that i can manually change it in inDesign, and that i didn´t know about it when i stared.

                              • 12. Re: Reference bug?
                                Pontus Uggla Level 1

                                Here is the deal, from the top.


                                We have an online database where our clients store all their data about their products.


                                They save everything from facts in different languages for different seasons, the save product pictures, image pictures, logotypes, what colors its in etc.


                                In the system we built an interface much like indesign, only to decide what product goes where.


                                The template is made in InDesign where you draw a placement box and inside you put textboxes, image boxes etc.


                                This client right now has sometimes none, sometimes 10 detail pictures for a product. The goal is not to make a finished design, just to but the correct data for someone to design with.


                                In the template I draw a rectangle and give it the label detail_images. The script ask the database about this product, okey it has 10 detail pictures. It then sets the order_by value of the label to 1, and then duplicates the box 9 times and updates the new rectangles so they get the correct label.


                                Lets say that the designer now by accident deletes all but to of these, or that new once was put in the database. Now when I run the update script, it has to duplicate the boxes again, after finding out what boxes are missing.


                                This is where I´m at now. The script looks through all rectangles and if the label matches it adds it to the array, and since i ran my label parsing function on it i pass that into the array as well.


                                Everything seems fine right now, I hace 10 rectangles, 8 are empty. Now another function goes through the rectangles and puts the pictures in the right box. And here is where the bug appears. If I alert the order_by value and select the box, everything is correct but one. That indicates a human error if it wasen´t for the fact that it points correctly if i just loop through them first and select them, or get some data from them or what ever.


                                This indicates a bug.


                                I´m building a much smaller script now that i hope will indicate that its not me that is very tired...