Setting instance names on keyframes quickly in AS3

Boon picture Boon · Jan 3, 2009 · Viewed 8.1k times · Source

Right now in Flash CS3 and up (using Actionscript 3) if you have the same instance that is used in multiple keyframes in a layer, and you decide to assign or change the instance name later, you would have to go to each keyframe and set the instance name. This is a big nuisance. Is there a quicker or better way to do this?

NOTE: In AS2, you can set the name by using name property of the MovieClip in your code in the onLoad handler of the MovieClip class so it's done once and for all. Unfortunately in AS3, you are not allowed to set the name property anymore.

Answer

Soviut picture Soviut · Jan 4, 2009

You can use JSFL, a javascript-based automation lanaguage in Flash, to automate tasks like this.

  • Click File > New
  • Select "Flash JavaScript File" from the list
  • Paste the following script
  • Make sure you have your instances selected in your FLA file
  • Click the Run (Play) button in your JSFL script file

You can then use the following code to name all selected instances with a prefix and an an index number:

var prefix:String = "myInstance_";
for(i in fl.getDocumentDOM().selection)
{
    fl.getDocumentDOM().selection[i].name = prefix + i.toString();
}

This will result in your instances being named myInstance_1, myInstance_2, etc. This is mainly an example for you to extend to your specific needs.

(One thing to note fl.trace() is how you print trace messages in JSFL when you're debugging, took me a while to figure that out)