Skip to main content
Inspiring
August 11, 2012
Answered

Swap symbol instance by changing its name?

  • August 11, 2012
  • 1 reply
  • 3842 views

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.

This topic has been closed for replies.
Correct answer Muppet Mark

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?


#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.symbol = red;

 

          }

 

          app.redraw();

 

};

1 reply

markerline
Inspiring
August 11, 2012

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.

Inspiring
August 11, 2012

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

markerline
Inspiring
August 11, 2012

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?


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;