Quantcast
Channel: Keith Hair » JSFL
Viewing all articles
Browse latest Browse all 4

Rename all instances with their library names

$
0
0

When creating a new symbol for MovieClips and other instances you must give them a name a second time again in the Properties panel. For me this can be annoying sometimes. (I name my instances the name I plan to later identify them with actionscript.)

Here is a modification of the "findObjects" I posted earlier. This is handy if you are the type of person that names your symbol items with names you intend to use as their instance name as well.

Maybe you will find this useful or know improvements, either way let me know your thoughts.

  1.  
  2. /*========================================================================
  3. selfNameAllInstances(true)
  4.  
  5. Renames all symbol instance names on the stage of in FLA document with
  6. the symbol's library Name.
  7.  
  8. Instances that already have a name will be skipped...
  9. If the argument "force_rename" is true, the element will be renamed
  10. regardless if it already had a instance name or not.
  11.  
  12. The renaming process is recursive to each
  13. for each MovieClip's timeline as well.
  14.  
  15. (Unfortunately symbols that are grouped are ignored.)
  16. =========================================================================*/
  17.  
  18. function selfNameAllInstances (force_rename)
  19. {
  20. fl.outputPanel.clear();
  21. var instanceName;
  22. var inInstance;
  23. var instances = [];
  24. var cnt=0;
  25. var findObjectInTimeline = function (instance, name)
  26. {
  27. var timeline;
  28. if (instance.toString () == "[object SymbolInstance]" || instance.toString () == "[object Frame]")
  29. {
  30. timeline = instance.timeline;
  31. }
  32. if (instance.toString () == "[object Document]")
  33. {
  34. timeline = instance;
  35. }
  36. if (instance.toString () == "[object Timeline]")
  37. {
  38. timeline = instance;
  39. }
  40. if (instance == fl.getDocumentDOM ())
  41. {
  42. timeline = instance.getTimeline ();
  43. }
  44. if (timeline == undefined)
  45. {
  46. return null;
  47. }
  48. var checked = new Object ();
  49. var currentLayers = timeline.layers;
  50. var layObj;
  51. var frm;
  52. var found;
  53. var i = 0;
  54.  
  55. while (i < currentLayers.length)
  56. {
  57. var layObj = currentLayers [i];
  58. var frms = layObj.frames;
  59. var k = 0;
  60. while (k < frms.length)
  61. {
  62. var selArry = frms [k].elements;
  63. var n = 0;
  64. while (n < selArry.length)
  65. {
  66. var obj = selArry [n];
  67. //=================================================================================
  68. //IF THE OBJECT HAS A "name" PROPERTY, ASSIGN IT TO "found" VARIABLE.
  69. //"found" VARIABLE will be returned from this recusive function.
  70. //---------------------------------------------------------------------------------
  71. if (obj.name != "undefined")
  72. {
  73. found = obj;
  74. instances.push (obj);
  75. }
  76. if (obj.libraryItem != undefined)
  77. {
  78. if (obj.libraryItem.name != undefined)
  79. {
  80. if (checked [obj.libraryItem.name])
  81. {
  82. n ++;
  83. continue;
  84. }else
  85. {
  86. checked [obj.libraryItem.name] = true;
  87. }
  88. //=================================================================================
  89. //RENAME INSTANCE ON STAGE WITH IT'S LIBRARY NAME
  90. //...also take of "slashes" in path.
  91. //---------------------------------------------------------------------------------
  92. var CHANGE_NAME=obj.libraryItem.name;
  93. if(obj.name=="" || force_rename){
  94. CHANGE_NAME=CHANGE_NAME.substr(CHANGE_NAME.lastIndexOf("/")+1,CHANGE_NAME.length);
  95. obj.name=CHANGE_NAME;
  96. cnt++;
  97. }
  98. //=================================================================================
  99. }
  100. obj = findObjectInTimeline (obj.libraryItem, name);
  101. if (obj != null)
  102. {
  103. found = obj
  104. }
  105. obj = selArry [n];
  106. if (obj.libraryItem.timeline != undefined)
  107. {
  108. obj = findObjectInTimeline (obj.libraryItem.timeline, name);
  109. if (obj != null)
  110. {
  111. found = obj
  112. }
  113. }
  114. }
  115. n ++;
  116. }
  117. k ++;
  118. }
  119. i ++;
  120. }
  121. return found;
  122. }
  123. if (inInstance == null)
  124. {
  125. inInstance = fl.getDocumentDOM ();
  126. }
  127. findObjectInTimeline (inInstance, instanceName);
  128. alert("Renamed "+cnt+" symbols.");
  129. }
  130.  
  131.  

Viewing all articles
Browse latest Browse all 4

Trending Articles