Link to home
Start Free TrialLog in
Avatar of jcsfrei
jcsfrei

asked on

automate find replace export

Hi all
I have a big collection of .fla files. Many of them have links to a special path on an actionscript in a button, through a window.open javascript call like window.open('../../../directory/file.htm')
But now I must change this path in all files to something like window.open('../directory/file.htm'). Unfortunately, I cant change the directory hierarchy to avoid changing the fla files.
Is there a way to automate this task?
I´ve read a few lines about jsfl, that you can see the history of your actions and so, but find/replace commands don't appear in history. Then I got lost in a huge api.
Using jsfl or even plain actionscript, is there a way of looping through all fla files in a given path, replace all ocurrences of the javascript call in each file and then export the swf of the modified fla, saving the files, instead of doing all this by hand? If possible, can you give any directions, hints, code snipets?
Best regards, JC
Avatar of wings_gaurav
wings_gaurav

hi,

it is certainly possible ussing jsfl - however it is not simple. the find and replace is not accesible to jsfl without a hack. for starters one can use code given below. the code is not complete neither have i run it ( have just typed in notepad :) ) if you have multiple timlines then one will have to add a loop for timelines. also, if the buttons are nested into other clips then one will have add a loop for items in library. hope this helps.

-gaurav

function replace(strContent)
{
      strContent.replace(/'..\/..\/..\/directory\/file.htm'/g, "../directory/file.htm");
}

function scanLayers(arrLayers)
{
      for(var i = 0; i < arrLayers.length ;i++)
      {
            var objLayer = arrLayers[i];
            for(var j = 0; j < objLayer.frames.length; j++)
            {
                  var objFrame = objLayer.frames[j];
                  replace(objFrame.actionScript);
                  
                  for(var k = 0; k < objFrame.elements.length; k++)
                  {
                        var objInstance = objFrame.elements[k];
                        if(objInstance.elementType == "instance" && 
                        objInstance.instanceType == "symbol")
                        {
                              replace(objInstance.actionScript);
                        }
                  }
            }
      }
}

function main()
{
      var strPath = "file:///c:/mypath/";
      
      var arrFiles = FLfile.listFolder(strPath + "*.fla","files");
      
      for(var i = 0; i < arrFiles.length ;i++)
      {
            var objDoc = fl.openDocument(strPath + arrFiles[i]);
            scanLayers(objDoc.timelines[0].layers);
            
            objDoc.save();
            objDoc.publish();
      }
}

main();

also, might want to add logging to this code. "objDoc.close()" after save will close documents - missed that!

-gaurav
Avatar of jcsfrei

ASKER

Hey gaurav
Thanks for your reply. I was already working on the code since no one answered. With your answer, I improved  my code. It searches through library and main timeline, has a flag to search only in the last frame or on all frames and a few twaeks. I'm working on an interface to put it as an extension.
Looks like this

FolderPath="file://c|/temp/";
findStr="../../test";
replaceStr="../test";
allframes=false;
if (FLfile.exists(FolderPath)){
      currFolder=FolderPath;
      URI = currFolder+"log.txt";
      FLfile.write(URI,"Start\r\n");
      var files = FLfile.listFolder(currFolder+"*.fla", "files");
      for(var f=0;f<files.length;++f) {
            fl.openDocument(currFolder+files[f]);
            FLfile.write(URI,":::"+currFolder+files[f]+"\r\n","append");
            if (doTimelines()){
                  fl.getDocumentDOM().save(true);
                  fl.getDocumentDOM().publish();
            }
            fl.getDocumentDOM().close(false);
      }
      FLfile.write(URI,":::End of folder contents.\r\n","append");
} else {                        
      alert("Folder not found.");
}

function doTimelines(){
      var found=false;
      if (sweepTimeline(fl.getDocumentDOM(),0)) found=true;
      var tline;
      for(var i = 0; i < fl.getDocumentDOM().library.items.length; i++){
            tline= fl.getDocumentDOM().library.items[i];
            if (sweepTimeline(tline,1)) found=true;
      }
      return found;
}
function sweepTimeline(n, isItem){
      var myTLine;
      var found=false;
      if (n.itemType=="video" || (n.itemType==undefined && isItem==1) || n.itemType=="folder" || n.itemType=="font" || n.itemType=="sound"
      || n.itemType=="bitmap" || n.itemType=="compiled clip") return found;
      if (isItem==1) { myTLine=n.timeline;}
      else {myTLine=n.getTimeline();}
      for(var i = 0; i < myTLine.layers.length; i++){
            var myLayers = myTLine.layers[i];
            if (myLayers.layerType!="normal") continue;
            var j=myLayers.frames.length-1;
            if (allframes) j=0;
            for (;j<myLayers.frames.length;j++){                  
                  var myFrames = myLayers.frames[j];
                  //test actionscript in frame
                  if (myFrames.actionScript!=undefined){
                        if (myFrames.actionScript.indexOf(findStr)>=0){
                              found=true;
                              if (isItem==1){
                                    fl.getDocumentDOM().library.editItem(myTLine.name);
                              }
                              myFrames.actionScript=myReplace(myFrames.actionScript,findStr,replaceStr);
                              FLfile.write(URI,myFrames.actionScript+"\r\n_____________________________________\r\n","append");                              
                        }
                  }                  
                  //test actionscript in each element in the frame
                  for(var k = 0; k <myFrames.elements.length; k++){
                        var myObj = myFrames.elements[k];
                        if (myObj!=undefined){
                              if (myObj.actionScript!=undefined){
                                    if (myObj.actionScript.indexOf(findStr)>=0){
                                          found=true;
                                          if (isItem==1){
                                                fl.getDocumentDOM().library.editItem(myTLine.name);
                                          }
                                          myObj.actionScript=myReplace(myObj.actionScript,findStr,replaceStr);
                                          FLfile.write(URI,myObj.actionScript+"\r\n_____________________________________\r\n","append");
                                    }
                              }
                        }
                  }
            }
      }
      return found;
}
function myReplace(str, c1, c2) {
      while (str.indexOf(c1)>=0){
            tmp0=str.slice(0,str.indexOf(c1))
            tmp1=str.slice(str.indexOf(c1)+c1.length,str.length);
            str=tmp0+c2+tmp1;
      }
      return str;
}

Problem: timeout alerts on a folder with many files and searches only on one timeline of the film, if it has many scenes, I thnk it does not scan all of them.
The timeout problem I'm trying to solve creating an swf that integrates the jsfl commands, but is very difficult to use actionscript and jsfl mixed together. I could not find any documentation that shows how do it. With an swf, I could use a tool that increases the timeout time.
Any thoughts?
Thanks, JC
ASKER CERTIFIED SOLUTION
Avatar of wings_gaurav
wings_gaurav

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jcsfrei

ASKER

hey gaurav
with your help I was able to fix and improve my code, and integrate the jsfl with action script. In the end, I managed to loop through all my flash files, change what I needed, exported the new swf files, generated a log file to track changes, saved the changed fla files and learned a lot about jsfl and flash extensions. Even managed to make my own panel and now its a default extension for the flash developers that work with me.
Thanks a lot for your help, a well deserved grade A.
Best, JC