Skip navigation
Currently Being Moderated

Swap symbol instance by changing its name?

Aug 11, 2012 12:58 AM

Tags: #learning_scripting #scripting #symbols

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.

 
Replies
  • Currently Being Moderated
    Aug 11, 2012 1:21 AM   in reply to David_Entwistle

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 11, 2012 2:24 AM   in reply to David_Entwistle

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

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 11, 2012 2:30 AM   in reply to David_Entwistle

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 11, 2012 5:51 AM   in reply to David_Entwistle
    #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();
      
    };
    
     
    |
    Mark as:
  • Currently Being Moderated
    Aug 11, 2012 6:34 AM   in reply to David_Entwistle

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 11, 2012 6:37 AM   in reply to David_Entwistle

    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;

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 11, 2012 6:54 AM   in reply to David_Entwistle

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 11, 2012 7:10 AM   in reply to David_Entwistle

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 11, 2012 7:18 AM   in reply to David_Entwistle

    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…

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points