7 Replies Latest reply on May 28, 2011 12:16 PM by kglad

    Component to Search AS2 Code

    bogusmoss

      I am trying to figure out a way to search the code and frames of an FLA and return the results that can be printed or at least displayed some way on screen.

       

      We have hundreds of Computer Based Training (CBT) Flash movies that need to be updated periodically. Each FLA has several frames which represent separate lessons. My dilemma is that in order to update an acronym, for instance, I'd have to open each FLA and look into each frame. I'd like to automate the process and be able to enter search criteria and then have the component spit out all instances of the acronym (or whatever) and also tell me on which frame each instance exists. To compound the issue external SWFs are loaded into some of the frames.

       

      Is there a way to do this with a component or maybe jsfl? Is there a way to report which frame the playhead is on when the search criteria is found? Although I think this is impossible, is there a way to jump into an externally loaded SWF and search the frames it contains for the search criteria?

       

      Any help is greatly appreciated.

       

      Thanks

        • 1. Re: Component to Search AS2 Code
          kglad Adobe Community Professional & MVP

          with jsfl and a fla, yes.  for a loaded swf, not directly.  you have to decompile the swf, first.

          • 2. Re: Component to Search AS2 Code
            bogusmoss Level 1

            Thanks for the quick response. I'm glad to see there may be a solution to my problem

             

            I'm not familiar with coding jsfl but I'm definitely ready to dive in and learn. Would you be able to share any good resources for jsfl with me or perhaps the methods I will need to conduct the search? Also, would I be able to build an interface with jsfl that allows me to input search criteria or can I build a component that runs the jsfl on the backend?

             

            I didn't think I'd be able to get into the SWF without decompiling. I have access to all the FLAs anyway, so no big deal. Thought I'd be able to streamline it further.

             

            Again, thanks for the quick response and for any help in the future.

            • 3. Re: Component to Search AS2 Code
              kglad Adobe Community Professional & MVP

              the flash help files contain all the code you can use.  once you get started it's pretty easy to use.

               

              create a new jsfl file starting with

               

              var doc = fl.getDocumentDOM();  // doc gets you a reference to the fla
              var rootTL = doc.getTimeline();   // then you're good to go.  this is a reference to the timeline of the doc (or root timeline).

               

              to run your jsfl file, click commands/run command from your fla.

               

              here's more code from one of my jsfl files.   because i work on fla's made by someone else they can (and often do) have code spread over dozens or even hundreds of timelines.

               

               

               

              var doc = fl.getDocumentDOM();
              var rootTL = doc.getTimeline();

               

              findAS(rootTL);  // find all the actionscript

              fl.saveDocumentAs(doc);  // i was search for actionscript of a particular kind and replacing it wherever it was found

               


              function findAS(tl) {
                  if(tl==rootTL){
                      for (var layerIndex = 0; layerIndex<tl.layers.length; layerIndex++) {
                          for (var frameIndex = 0; frameIndex<700; frameIndex++) {
                          //for (var frameIndex = 0; frameIndex<tl.layers[layerIndex].frameCount; frameIndex++) {
                              //fl.trace(frameIndex+" : "+tl.layers[layerIndex].frameCount);
                             
                              if(tl.layers[layerIndex].frames[frameIndex]!=undefined&&tl.layers[layerIndex].frames[fram eIndex]!=null){
                                  //fl.trace(tl.name+" : "+tl.layers[layerIndex].name+" "+frameIndex);
                                  findASinLayerFrame(tl, frameIndex, layerIndex);
                              }
                          }
                      }
                  } else {
                      for (var layerIndex = 0; layerIndex<tl.layers.length; layerIndex++) {
                          for (var frameIndex = 0; frameIndex<tl.layers[layerIndex].frameCount; frameIndex++) {
                              findASinLayerFrame(tl, frameIndex, layerIndex);
                          }
                          // end frameIndex
                      }
                      // end layerIndex
                  }
              }

              • 4. Re: Component to Search AS2 Code
                bogusmoss Level 1

                Thanks for the help. I think I'm well on my way now but I have one more question. Thanks to your code I am able to drill down into each layer and get the number of frames and select any frame that I want but the one thing that still eludes me is how to search for specific AS code or even specific text that's designated for a static or dynamic textbox. I found the actionsPanel object and I can pull all of the actionScript from the FLA but I wonder if this is absolutely necessary. I've also tried the findObjectInDocByType method but for some reason I get an error that says it is not a function even though the documentation states that it takes two arguments (type and document). I'm using CS5 by the way, not sure if that matters. Please let me know if I'm barking up the wrong tree on this. Again, thanks in advance for any help.   

                • 5. Re: Component to Search AS2 Code
                  kglad Adobe Community Professional & MVP

                  well, here's more of that code.  it's from a jsfl file that finds specific actionscript strings and replaces it with other actionscript.  all timelines that are used or could be used are searched for actionscript:

                   

                  var doc = fl.getDocumentDOM();
                  var rootTL = doc.getTimeline();
                  var checked_mcA = [];

                   

                  // *** begin initializations
                  // array of replacement arrays.  first term is searched and replaced by 2nd.
                  // example:
                  //var replacementA = [["new Sound()","new Sound(this)"],[" 'audio/","pathS+'audio/"]];
                  //
                  var replacementA = [["._x+357","._x+350"]];

                   


                  // *** end initializations

                   

                  //fl.trace(doc.name);
                  findAS(rootTL);
                  //findLibrary_mcAS();
                  //fl.saveDocumentAs(doc);
                  fl.saveDocumentAs(doc);

                   


                  function findAS(tl) {
                      if(tl==rootTL){
                          for (var layerIndex = 0; layerIndex<tl.layers.length; layerIndex++) {
                              for (var frameIndex = 0; frameIndex<700; frameIndex++) {
                              //for (var frameIndex = 0; frameIndex<tl.layers[layerIndex].frameCount; frameIndex++) {
                                  //fl.trace(frameIndex+" : "+tl.layers[layerIndex].frameCount);
                                 
                                  if(tl.layers[layerIndex].frames[frameIndex]!=undefined&&tl.layers[layerIndex].frames[fram eIndex]!=null){
                                      //fl.trace(tl.name+" : "+tl.layers[layerIndex].name+" "+frameIndex);
                                      findASinLayerFrame(tl, frameIndex, layerIndex);
                                  }
                              }
                          }
                      } else {
                          for (var layerIndex = 0; layerIndex<tl.layers.length; layerIndex++) {
                              for (var frameIndex = 0; frameIndex<tl.layers[layerIndex].frameCount; frameIndex++) {
                                  findASinLayerFrame(tl, frameIndex, layerIndex);
                              }
                              // end frameIndex
                          }
                          // end layerIndex
                      }
                  }
                  ////////////////////////
                  function findASinLayerFrame(tl, frameIndex, layerIndex) {
                      //fl.trace(tl.layers[layerIndex].name+" :: "+layerIndex+" ::: "+frameIndex+" "+tl.layers[layerIndex].frames[frameIndex].startFrame);
                      if (tl.layers[layerIndex].frames[frameIndex].startFrame == frameIndex) {

                   

                          // edit actionscript, if any, attached to this frame/layer.  actionscript only on keyframes (hence, start_frame=frameIndex)
                          var as = tl.layers[layerIndex].frames[frameIndex].actionScript;
                          if (editNeeded(as)) {
                              //fl.trace("frameIndex: "+frameIndex);
                              as = editAS(as);
                              //fl.trace(tl.name);
                          }
                          tl.layers[layerIndex].frames[frameIndex].actionScript = as;
                          //fl.trace("****");
                          // find components/movieclips/buttons that could have actionscript attached to this tl/layer/frame
                          //elementA = tl.layers[layerIndex].frames[frameIndex].elements;
                          //fl.trace(tl.layers[layerIndex].frames[frameIndex].elements.length);
                          for (var eNum = 0; eNum<tl.layers[layerIndex].frames[frameIndex].elements.length; eNum++) {
                              if (tl.layers[layerIndex].frames[frameIndex].elements[eNum].elementType == "instance") {
                                  libItem = tl.layers[layerIndex].frames[frameIndex].elements[eNum].libraryItem;
                                 
                                  //fl.trace(tl.layers[layerIndex].frames[frameIndex].elements[eNum].libraryItem.name+" "+tl.layers[layerIndex].frames[frameIndex].elements[eNum].elementType+" "+tl.layers[layerIndex].frames[frameIndex].elements[eNum].libraryItem.itemType)
                                  // check for code directly attached to this instance, if button
                                  // anything in library derives from Item object.  item objects have itemType property
                                  // tl.layers[layerIndex].frames[frameIndex].elements[eNum] is a SymbolInstance and therefore as an actionScript property
                                  if (libItem.itemType == "button") {
                                      if (editNeeded(tl.layers[layerIndex].frames[frameIndex].elements[eNum].actionScript)) {
                                          tl.layers[layerIndex].frames[frameIndex].elements[eNum].actionScript = editAS(tl.layers[layerIndex].frames[frameIndex].elements[eNum].actionScript);
                                      }
                                  }
                                  // check for code directly attached to this instance, if component
                                  if (libItem.itemType == "component") {
                                      if (editNeeded(tl.layers[layerIndex].frames[frameIndex].elements[eNum].actionScript)) {
                                          tl.layers[layerIndex].frames[frameIndex].elements[eNum].actionScript = editAS(tl.layers[layerIndex].frames[frameIndex].elements[eNum].actionScript);
                                      }
                                  }
                                  if (libItem.itemType == "movie clip") {
                                      // check for code directly attached to this instance, if movieclip
                                      if (editNeeded(tl.layers[layerIndex].frames[frameIndex].elements[eNum].actionScript)) {
                                          tl.layers[layerIndex].frames[frameIndex].elements[eNum].actionScript = editAS(tl.layers[layerIndex].frames[frameIndex].elements[eNum].actionScript);
                                      }
                                      // and then check if this is a movieclip that has code on its timeline  
                                      // or other movieclips/buttons on its timeline etc
                                      // linked library items checked from findLibrary_mcAS.  relative path difficult because library items may be attached to any timeline
                                      // and in fact, the same library item may be attached to different timelines and have different relative paths
                                      if (!memberOfA(checked_mcA, libItem)) {
                                          //fl.trace("libItem "+libItem.name);
                                          checked_mcA.push(libItem);
                                          findAS(libItem.timeline);
                                      }
                                  }
                                  //end itemType = "movie clip"
                              }
                              // end if-elementyType=="instance"
                          }
                          //end eNum loop
                      }
                      // end if-startFrame
                  }
                  ///////////////////////
                  function findLibrary_mcAS(){
                      libraryItemsA = doc.library.items;
                      for (var libItemNum = 0; libItemNum<libraryItemsA.length; libItemNum++) {
                          libraryItem = libraryItemsA[libItemNum];
                          if (libraryItem.linkageExportForAS && !memberOfA(checked_mcA,libraryItem) && libraryItem.itemType == "movie clip") {
                              checked_mcA.push(libraryItem);
                              findAS(libraryItem.timeline);
                          }
                      }
                  }

                   

                  function editAS(as){
                      for(var rIndex=0;rIndex<replacementA.length;rIndex++){
                          as = as.split(replacementA[rIndex][0]).join(replacementA[rIndex][1]);
                      }
                      return as;
                  }

                   

                  function editNeeded(as){
                      if(as==undefined||as==null){
                          return false;
                      } else {
                          if(as.length>0){
                              for(var rIndex=0;rIndex<replacementA.length;rIndex++){
                                  if(as.indexOf(replacementA[rIndex][0])>-1 && as.indexOf    (replacementA[rIndex][1])==-1){
                                      return true;
                                  }
                              }
                              // as defined, as.length>0, no replacement strings found
                              return false;
                          } else {
                              // as defined,as.length==0
                              return false;
                          }
                      }
                  }

                   

                  function memberOfA(a,e){
                      for(i=0;i<a.length;i++){
                          if(a[i]==e){
                              return true;
                          }
                      }
                      return false;
                  }

                  • 6. Re: Component to Search AS2 Code
                    bogusmoss Level 1

                    Thank you very much. You are a great asset to these forums.

                     

                    I've gotten through a portion of the code you have given me and this is definitely going to help in my efforts. Thanks again for the help and have a great weekend!

                    • 7. Re: Component to Search AS2 Code
                      kglad Adobe Community Professional & MVP

                      you're very welcome.