14 Replies Latest reply: Aug 11, 2012 7:18 AM by Muppet Mark RSS

    Swap symbol instance by changing its name?

    David_Entwistle Community Member

      Hello all

       

      I'm trying to swap symbols in situ by changing the 'name' property of the symbol instance.

       

      My rudimentary script can find the existing symbol name (for me, that's a small triumph!). The Illustrator document has two symbols in the Symbols panel, 'blue_square' and 'red_circle'. I run the script with an instance of 'blue_square' (and nothing else) on the page, and try to change the name to 'red_circle':

       

      var docRef = activeDocument;

       

      var symbolitem = docRef.symbolItems[0]

       

      var symbolname = symbolitem.symbol.name;

       

      var newname = "red_circle";

       

      symbolitem.selected = true;

       

      // this next line causes the problem:

      symbolitem.symbol.name = newname;

       

      This causes an error - 'the name is in use'. Could someone please tell me where I'm mistaken?

       

      Many thanks for your interest.

        • 1. Re: Swap symbol instance by changing its name?
          markerline Community Member

          I know little about Javascript as it pertains to AI .  However with my experience in Actionscript, albeit very limited, try this :

           

           

          // this next line causes the problem

          symbolitem.symbol.name = newname;

           

           

          Change that to:

           

          //

          symbolname = newname;

           

          You have declared symbolname as a variable above so I'm thinking this is where it's causing an error.

          • 2. Re: Swap symbol instance by changing its name?
            David_Entwistle Community Member

            Hi markerline

             

            Sadly, despite both our hopes, that didn't work. I think it changes the name in the variable, but the property name is left unchanged. But thank you indeed for the suggestion - very grateful.

             

            David Entwistle

            • 3. Re: Swap symbol instance by changing its name?
              markerline Community Member

              can you post your entire script, especially the part that reads the name of the blue square back to you in a dialog box?

              • 4. Re: Swap symbol instance by changing its name?
                markerline Community Member

                actually the script works for me in the following context.  firstly you are missing a semicolon at the end of one of your first few lines.  secondly i am using the name symbolItemName as a variable instead of symbolItem.  i am also using aics6

                • 5. Re: Swap symbol instance by changing its name?
                  David_Entwistle Community Member

                  Here's the script including the 'alert' line which confirms that I'm picking up the symbol instance's initial name (in this case, 'blue_square'):

                   

                  var docRef = activeDocument;

                   

                  var symbolitem = docRef.symbolItems[0];

                   

                  var symbolname = symbolitem.symbol.name;

                   

                  alert ("the symbol's name is " + symbolname)

                   

                  var symbolNewName = "red_circle";

                   

                  symbolitem.selected = true;

                   

                  // this next line causes a problem:

                  symbolitem.symbol.name = symbolNewName;

                   

                  Thank you for the syntax correction. I've tried a variety of variable names for the new  symbol name, including the one you suggested, but I still get the same error that 'the name [meaning 'red_circle'] is in use'.

                   

                  But I'm interested that it may work in CS6, as I'm still in CS4. Does it actually change the symbol on the page for you, which is what I'm trying to do?

                  • 6. Re: Swap symbol instance by changing its name?
                    Muppet Mark Community Member
                    #target illustrator
                    
                    symbolSwapper()
                    
                    function symbolSwapper() {
                      
                              var doc, count, red, blue, i;
                      
                              doc = app.activeDocument;
                      
                              count = doc.symbolItems.length;
                      
                              red = doc.symbols.getByName( 'Red Circle' );
                      
                              for ( i = 0; i < count; i++ ) {
                      
                                        doc.symbolItems[i].symbol = red;
                      
                              }
                      
                              app.redraw();
                      
                    };
                    
                    • 7. Re: Swap symbol instance by changing its name?
                      markerline Community Member

                      The script does not change the symbol itself just the symbol's name.  Muppet Mark's symbol script may actually do both.  I haven't tested it and I can't right now.  Have a look on a search engine for Adobe Illustrator CS6 Scripting and search for the JavaScript Guide or Handbook (or whatever it's called, Reference maybe?).  It should be an adobe link that you're finding and the Javascript Guide is a PDF document that you can save to your hard drive for easy access.

                      • 8. Re: Swap symbol instance by changing its name?
                        markerline Community Member

                        var docRef = activeDocument;

                         

                        // your previous variable name may have been a keyword.  also, did you check in your symbol gallery whether the name was // changed to "red_circle"?

                        var symbolitemName = docRef.symbolItems[0];

                         

                        var symbolname = symbolitemName.symbol.name;

                         

                        alert ("the symbol's name is " + symbolname)

                         

                        // this can be anything that is not being used as a keyword

                        var symbolNewName = "red_circle";

                         

                        symbolitemName.selected = true;

                         

                        // this next line causes a problem:

                        symbolitemName.symbol.name = symbolNewName;

                        • 9. Re: Swap symbol instance by changing its name?
                          David_Entwistle Community Member

                          Ah, this works. *Thank you* for your expertise. I can't say that I understand entirely how it works, but I'm going to enjoy finding out.

                           

                          Very grateful!

                          • 10. Re: Swap symbol instance by changing its name?
                            markerline Community Member

                            I have verified the following:

                             

                            1) Muppet Mark's script actually changes the symbol due to his use of the app.redraw() function in addition to the other bits of code he used leading up to that.

                            2) Your script when properly syntaxed (with variable names not conflicting with keywords) changes the name of the symbol but does not change the symbol that is on the stage.

                             

                            In other words in the first case you have drawn a blue square and a red circle onto the stage and converted each of them to a symbol (Movie Clip is fine) with the name "blue_square" and "Red_Circle".  Note the underline in Red_Circle which you will have to amend in Muppet Mark's script in order not to get any errors.  Delete the original symbols from the stage as they are in your library and drag new ones to the stage from your symbol library, one of each.  give them instance names (I don't know if this step is optional or not).  You will see that you now have two red circles on the stage after running Muppet Mark's script.

                             

                            In the second case you do exactly as the first case but you will only notice that the symbol name "Red_Circle" changes to "red_circle" when running your script but no symbol instances are changing on the stage because no app.redraw() is being called.  Perhaps the nature of the script might not allow for any redraw anyway but I can verify that both scripts work and do what I have described in the previous two paragraphs.

                            • 11. Re: Swap symbol instance by changing its name?
                              David_Entwistle Community Member

                              Hi markerline - thank you too for your expertise. I've learned a lot about checking for keywords.

                              • 12. Re: Swap symbol instance by changing its name?
                                David_Entwistle Community Member

                                …and even more thanks for your last reply, which I hadn't seen when I said 'thanks'. So even more thanks now - there's no adequate way to convey the sense of gratitude you feel when someone puts themselves out to solve a problem for you.

                                • 13. Re: Swap symbol instance by changing its name?
                                  markerline Community Member

                                  you're quite welcome and the following does the same thing that Muppet Mark's script does but with less elegance since he contained his steps within a function and you did not... but it still works nonetheless so that is positive.

                                   

                                  var docRef = activeDocument;


                                  // your previous variable name may have been a keyword. 
                                  // also, did you check in your symbol gallery whether the name was
                                  // changed to "red_circle"?

                                  var symbolitemName = docRef.symbolItems[1]; //this can be either index 1 or index 0 depending on the order in which you created your symbols and placed them on the stage.


                                  var symbolname = symbolitemName.symbol.name;


                                  alert ("the symbol's name is " + symbolname)


                                  // this can be anything that is not being used as a keyword

                                  var symbolNewName = docRef.symbols.getByName("Red_Circle");


                                  symbolitemName.selected = true;


                                  // this next line causes a problem:

                                  //symbolitemName.symbol.name = symbolNewName; //notice that you are changing the name prop here
                                  symbolitemName.symbol = symbolNewName; //notice that you are changing the symbol prop here.


                                  app.redraw();

                                   

                                  Message was edited by: markerline

                                  • 14. Re: Swap symbol instance by changing its name?
                                    Muppet Mark Community Member

                                    You will see that I have wrapped all my syntax in a function… This is good practice with scripting AI as the engine is persistent throughout the session… Declaired all my variables at the off… inside the function wrapper. I didn't bother to check that a document was open nor try/catch the getByName() which you should… I knew my doc was open and contained what I wanted…