14 Replies Latest reply on Dec 26, 2011 5:07 PM by John Hawkinson

    How can I set a polygon's path by using the .add() function?

    RyanPPI

      I have been searching the web and this forum and doing plenty of experimenting, and I can't for the life of me figure out how to do this.

       

      I can set other attributes of a polygon inside the add() parentheses, such as the fillColor, but I can't set the polygon's path's pathPoints unless I use a separate line of code after the add(); line.

       

      My script adds a great deal of polygons in a row, so halving the number of operations would greatly help me, and also it seems that some properties such as appliedObjectStyle cannot be set until after a polygon's path is defined. That means I need a third line of code after add() and entirePath=, which slows me down even more.

       

      Help?

        • 1. Re: How can I set a polygon's path by using the .add() function?
          John Hawkinson Level 5

          Posting your code would help.

          I don't see a pathPoints property in a Polygon. What are you referring to exactly?

          • 2. Re: How can I set a polygon's path by using the .add() function?
            [Jongware] Most Valuable Participant

            pathPoints are not a property of the polygon, but of its paths (plural). It's probably a problem because you are trying to change something 'two levels deep'.

             

            This does work, because polygons.add returns the newly created polygon:

             

            p = [ [0,0], [10,0], [10,10], [0,10] ];
            app.activeWindow.activePage.polygons.add ({fillColor:app.activeDocument.swatches.item("Black")}).paths[0].entirePath = p;
            

             

            ... I don't think this would lead to a massive speed increase.

            • 3. Re: How can I set a polygon's path by using the .add() function?
              RyanPPI Level 1

              That is helpful, but I'd also like to set other things about the polygon in the add() parenthesis as well, such as the drop shadow.

               

              Is there some way to set properties of the polygon, that are more than one level 'distant' from the polygon itself, inside the add() parentheses?

               

              For example, something like this doesn't work, because the drop shadow angle is not a property of the polygon itself:

              app.activeWindow.activePage.polygons.add ({fillColor:"FireRed", transparencySettings.dropShadowSettings.angle:120});

               

              It doesn't seem to want me to access attributes of the polygon that require a period, only ones that are directly able to be set about the polygon itself.

               

              However, since transparencySettings are a property of the polygon, and dropShadowSettings are a property of the transparencySettings, and angle is a property of the dropShadowSettings, shouldn't I still be able to set it in the add() parentheses somehow?

               

              The point is I'd like to set a bunch of attributes like that all with one call, since I'm doing this a lot in my script.

              • 4. Re: How can I set a polygon's path by using the .add() function?
                Marc Autret Level 4

                RyanPPI wrote:

                 

                […]

                For example, something like this doesn't work, because the drop shadow angle is not a property of the polygon itself:

                app.activeWindow.activePage.polygons.add ({fillColor:"FireRed", transparencySettings.dropShadowSettings.angle:120});

                 

                It doesn't seem to want me to access attributes of the polygon that require a period, only ones that are directly able to be set about the polygon itself.

                 

                However, since transparencySettings are a property of the polygon, and dropShadowSettings are a property of the transparencySettings, and angle is a property of the dropShadowSettings, shouldn't I still be able to set it in the add() parentheses somehow?

                 

                […]

                 

                Try this:

                 

                app.activeWindow.activePage.polygons.add({
                    fillColor:"FireRed",
                    transparencySettings: {dropShadowSettings: {angle:120}}
                    });
                

                 

                @+

                Marc

                1 person found this helpful
                • 5. Re: How can I set a polygon's path by using the .add() function?
                  RyanPPI Level 1

                  Marc,

                   

                  That works, but only works for setting the one property... as drop shadows have many properties, I need to set more than one.

                   

                  This doesn't seem to work:

                  app.activeWindow.activePage.polygons.add({
                      fillColor:"FireRed",
                      transparencySettings: {dropShadowSettings: {angle:120}}

                      transparencySettings: {dropShadowSettings: {distance:1}}
                      });

                   

                   

                  Nor does this:

                  app.activeWindow.activePage.polygons.add({
                      fillColor:"FireRed",
                      transparencySettings: {dropShadowSettings: {properties: {

                      angle:120,

                      distance:1 }}}

                      });

                   

                   

                  What am I doing wrong?

                  • 6. Re: How can I set a polygon's path by using the .add() function?
                    RyanPPI Level 1

                    Also, it doesn't let me use square brackets or parentheses inside the add() parentheses, so the original problem I posted (trying to set a polygon's path) is still a problem.

                     

                    As I need to say either paths[0].entirePath or paths.item(0).entirePath or paths.firstItem().entirePath to refer to the path, I'm unable to set the path inside the add() parentheses.

                     

                    Ideas?

                    • 7. Re: How can I set a polygon's path by using the .add() function?
                      John Hawkinson Level 5

                      Ryan, I think you need to reset and take a moment to understand what it is that is going on here.

                      The things you've proposed that don't work shouldn't work, and it's all quite simple.

                       

                      Generally speaking, a .add() function takes an additional parameter, a JavaScript Object, that list properties of the created object that can be set.

                      A JavaScript Object is a list of key/value pairs, just like an associative array in a language like perl, or a dictionary in a language like Python. It is expressed in curly braces as such:

                       

                      { key1: value1, key2: value2, key3: value3}
                      

                       

                      So, for any such general add function, these

                       

                      foo = whatever.add();
                      foo.bar = baz;
                      

                       

                      are equivalent to this:

                       

                      foo = whatever.add({bar: baz});
                      

                       

                      Are you with me? So, for instance, you had originally asked why you could not use pathPoints in polygons.add() and the answer is simpe. You cannot set polygon.pathPoints, because there is no .pathPoints property of a polygon.

                       

                      So, when you want to try add properties inside properties, you must do as as objects within objects, and follow the strict hierarchy. Marc advises that this works:

                      app.activeWindow.activePage.polygons.add({

                          fillColor:"FireRed",
                          transparencySettings: {dropShadowSettings: {angle:120}}
                          });

                       

                      and if indeed that is so, then the extension for setting multiple transparencySettings should be clear. It is not this, that you propose:

                      app.activeWindow.activePage.polygons.add({
                          fillColor:"FireRed",
                          transparencySettings: {dropShadowSettings: {angle:120}}
                          transparencySettings: {dropShadowSettings: {distance:1}}
                          });
                      

                      Because to do so is to set the transparencySettings key twice in the same Object. And to do that is to replace the first with the second. The above (yours) is wholly equavelent to:

                      app.activeWindow.activePage.polygons.add({
                          fillColor:"FireRed",
                      
                          transparencySettings: {dropShadowSettings: {distance:1}}
                          });
                      

                      If you wish to set more than one attribute of dropShadowSettings, you must set transparencySettings to an object containing one and only one dropShadowSettings, and you must do it only once. So it is this:

                      app.activeWindow.activePage.polygons.add({
                          fillColor:"FireRed",
                          transparencySettings:
                            {dropShadowSettings: {angle:120, distance: 1}}
                          });
                      

                      I am not sure why you thought you should have the name of the key in the Object named properties. That is probably because in some cases you can use:

                      foo.properties = { a: 1, b: 2};
                      

                      as a shorthand for

                      foo.a=1;
                      foo.b=2;
                      

                      but you would [probably] never want to mix that with the object notation for setting multiple properties in the .add() function.

                       

                      Does this help to clarify?

                       

                      As for your last question:

                      Also, it doesn't let me use square brackets or parentheses inside the add() parentheses, so the original problem I posted (trying to set a polygon's path) is still a problem.

                      It's not about the use of brackets or parentheses, but what they mean and where they go. As you have not posted an example of setting the polygon's path the long way, it's hard to show you how to shorten it. Post what you have that works, and we will show you how to shorten it. (I suppose some with more patience than I are willing to go look up what we think it is you are trying to do, and then interpret it. But I would much rather you show me the code you have that works, and then your attempts to transform or shorten it and change its notation. This makes it much much easier to help you, and it should also make the help more effective, by contextualizing it. As an added benefit, when someone else reads your post and tries to learn from it, they will gain more.)

                       

                      So again, please provide a clear example of the "long way" to do the thing you are attempting, and then your attempt at shortening it.

                      1 person found this helpful
                      • 8. Re: How can I set a polygon's path by using the .add() function?
                        RyanPPI Level 1

                        John,

                         

                        Thank you for your very informative and detailed reply, I really appreciate you taking the time to help me learn where I'm going wrong.

                         

                        I totally understand the dropshadow stuff (and thus any other 'extended' properties that have more than one sub-property... if that makes sense).

                         

                        Here is my 'long way' of setting a polygon's path:

                        myPoly = app.activeWindow.activePage.polygons.add();

                        myPoly.paths[0].entirePath = [ [0,0], [10,0], [10,10], [0,10] ] ;

                         

                        I would like to do it inside the add() parenthesis along with all the other things I'm setting such as the dropshadow, like so (dropshadow, etc removed for simplicity):

                        app.activeWindow.activePage.polygons.add ({

                        paths[0]: {entirePath: { [ [0,0], [10,0], [10,10], [0,10] ] } }

                        });

                         

                        But it chokes as soon as it hits the left square bracket after paths. All of the methods to say which path I'm referring to require either square brackets or parentheses, so is there any way around this?

                        • 9. Re: How can I set a polygon's path by using the .add() function?
                          [Jongware] Most Valuable Participant

                          After some twenty-odd tries I finally got the syntax right, but ID flatly refuses to 'apply' the new path:

                           

                          app.activeWindow.activePage.polygons.add ({
                                    geometricBounds:[ 0, 0, 20, 20 ],
                                    paths:[ {entirePath:[ [0,0], [10,0], [10,10], [0,10] ] } ]
                            }
                          );
                          

                           

                          I recall encountering other circumstances where 'with properties' commands fail to set all of the new attributes. It could be as simple as the internal creation order -- one cannot change the entire path until the polygon is created with its initial default path.

                           

                          But it seems you are putting too much faith in 'one-liners' anyway. Does doing this in two lines really take lots of extra time, or is it just nagging you it can't be done? How many polygons do you need to create, and how often do you need to do that?

                          • 10. Re: How can I set a polygon's path by using the .add() function?
                            RyanPPI Level 1

                            Jongware,

                             

                            I've noticed that you can put nonsense properties inside the curly braces and it won't complain, so maybe even though the syntax is right, maybe you're just not telling it to do anything intelligible?

                             

                            I notice that you're not declaring which path it is you're trying to edit, so I suspect you're having the same problem as me (every method I know to declare which path you want needs a round or square bracket in a place it doesn't like if you're inside the add() parentheses).

                             

                            Your example is equivalent to:

                            myPoly = app.activeWindow.activePage.Polygons.add();

                            myPoly.paths.entirePath = [ [0,0], [10,0], [10,10], [0,10] ];

                             

                            Doing so won't actually set the path, since you haven't said which path (inside that polygon's paths collection) you're trying to set.

                             

                            As for my faith in one-liners, it's kind of both. It will end up as extra time, and also it's bothering me that I can't get it to work. I'm trying to learn how to set attributes that require defining one element of an array inside the add() parenthesis, and this is just one example I'm using to learn how to do so. So it's not as much about how many polygons I have to create as it is about trying to learn this technuque that I can then apply all over the place in my various scripts to speed things up.

                             

                            Anyone else figured this out?

                            • 11. Re: How can I set a polygon's path by using the .add() function?
                              Marc Autret Level 4

                              Hi Jongware,

                               

                              I understand what you are trying to do, but I don't think it's possible to set up a pure collection through the add( ) method of any object. The add() method usually supports creation properties—i.e. what we can address through the myItem.properties object—but the paths property is not a member of Polygon.properties, as demonstrated by:

                               

                              alert( myPolygon.properties.toSource() );

                               

                              So IMHO your first suggestion was the best. I don't know any way to setup the path of a polygon during its creation.

                              Here is a concrete example with various creation properties:

                               

                              app.activeWindow.activePage.polygons.add({
                                  fillColor:"FireRed",
                                  transparencySettings: {
                                      dropShadowSettings: { angle:120, distance:1 },
                                      innerGlowSettings: { size: 10, applied: true }
                                      },
                                  localDisplaySetting: DisplaySettingOptions.HIGH_QUALITY
                                  }).paths[0].entirePath = [[0,0],[50,50],[0,50]];
                              

                               

                              Curiously, it seems that the applied property is missing in the DropShadowSettings object, whereas it is available in the other effects. Why?

                               

                              @+

                              Marc

                              • 12. Re: How can I set a polygon's path by using the .add() function?
                                John Hawkinson Level 5

                                Perhaps I'm repeating covered ground, but:

                                 

                                 

                                Here is my 'long way' of setting a polygon's path:

                                myPoly = app.activeWindow.activePage.polygons.add();

                                myPoly.paths[0].entirePath = [ [0,0], [10,0], [10,10], [0,10] ] ;

                                OK, great.

                                You offer:

                                app.activeWindow.activePage.polygons.add ({

                                paths[0]: {entirePath: { [ [0,0], [10,0], [10,10], [0,10] ] } }

                                });

                                No way that could work! You have "{ [ [0,0] ...". But { introduces an Object, and every object must contain key/value pairs. You have values but no keys! If EntirePath is to be an array, as in your above example, then you must omit the curly brances surrounding its values. Anyhow, Jongware seems to have translated it almost-correctly.

                                 

                                Additionally, though, you have "paths[0]" as the key to the outer Object. That cannot work either -- a key must be a scalar. It cannot be a reference to an array that does not already exist. But remember

                                 

                                array[0] = foobar;

                                 

                                it really the same as

                                 

                                array = [ foobar ];

                                 

                                It sets the first element of the array to the given value.

                                 

                                Anyhow, you suggest Jongware's translation:

                                 

                                app.activeWindow.activePage.polygons.add ({
                                          geometricBounds:[ 0, 0, 20, 20 ],
                                          paths:[ {entirePath:[ [0,0], [10,0], [10,10], [0,10] ] } ]
                                  }
                                );

                                 

                                is equivalent to:

                                 

                                myPoly = app.activeWindow.activePage.Polygons.add();

                                myPoly.paths.entirePath = [ [0,0], [10,0], [10,10], [0,10] ];

                                 

                                But that is not correct. In Jongware's example, paths is an array containing an object. That is, it is really equivalent of:

                                 

                                myPoly = app.activeWindow.activePage.Polygons.add();

                                myPoly.paths = [ { entirePath: [ [0,0], [10,0], [10,10], [0,10] ] } ];

                                 

                                I am not sure that you can create a Path with initialized properties that way. I'm afraid I don't quite have the chance to fiddle with this right now, but I would start by trying to initialize a path without adding it to the polygon and make sure it works.

                                • 13. Re: How can I set a polygon's path by using the .add() function?
                                  RyanPPI Level 1

                                  John,

                                   

                                  I understand now, and it seems that what I'm trying to do is not possible. I suspect it's because of what Marc said:

                                   

                                  I understand what you are trying to do, but I don't think it's possible to set up a pure collection through the add( ) method of any object. The add() method usually supports creation properties—i.e. what we can address through the myItem.properties object—but the pathsproperty is not a member of Polygon.properties, as demonstrated by:

                                   

                                  alert( myPolygon.properties.toSource() );

                                   

                                  As such, I'll be using the workaround Marc suggested in that same post, as it allows me to do what I'm doing in one step anyways:

                                   

                                   

                                  app.activeWindow.activePage.polygons.add({
                                      fillColor:"FireRed",
                                      transparencySettings: {
                                          dropShadowSettings: { angle:120, distance:1 },
                                          innerGlowSettings: { size: 10, applied: true }
                                          },
                                      localDisplaySetting: DisplaySettingOptions.HIGH_QUALITY
                                      }).paths[0].entirePath = [[0,0],[50,50],[0,50]];

                                   

                                   

                                  Thanks everybody!

                                  • 14. Re: How can I set a polygon's path by using the .add() function?
                                    John Hawkinson Level 5

                                    Note that while what you have there is a single line of Javascript, I don't think it is any more or less steps than writing it in two lines of Javascript. It will be just as much back-and-forth with the DOM, but as you have written it, compactly, it will just be harder for anyone else to read (but you will save a variable). I think it would be best to write it as two "lines."

                                     

                                     

                                    I finally had a chance ot fiddle with the DOM. I think you are correct to conclude what you want is not feasible.

                                     

                                    Incidently, one can specify the creation properties of Path like this:

                                     

                                    p.paths.add({ entirePath: [ [0,0],[50,50],[0,50] ] });
                                    

                                     

                                    And one cannot, somewhat to my surprise, offer an existing collection of Path objects to Polygons.add(); this fails:

                                     

                                    p0 = app.activeWindow.activePage.polygons.add();
                                    p0.paths[0].entirePath = [ [0,0],[50,50],[0,50] ];
                                    p=app.activeWindow.activePage.polygons.add({
                                        fillColor:"Black",
                                        transparencySettings: {
                                            dropShadowSettings: { angle:120, distance:1 },
                                            innerGlowSettings: { size: 10, applied: true }
                                            },
                                        localDisplaySetting: DisplaySettingOptions.HIGH_QUALITY,
                                        paths: p0.paths,
                                        });
                                    

                                     

                                    It also fails using p0.paths[0].getElement() and p0.paths.everyItem().getElement().

                                     

                                    I still think the general advice about optimization in computer programming is good advice -- don't bother until you have a problem, and then use a time profiling tool (such as the ESTK offers) to determine where your program is slowest, and optimize that. Premature optimization just leads to unreadable code, and often we are wrong in our intuition about what parts are slow. So write your code for clarity!