Apollo File Filter and rename app

Here’s some sample code for those trying out Adobe Apollo (Application wrapper for Adobe Flash and Javascript code) – a simple application to allow you to keep every nth file from an image sequence and creates a subdirectory where it renames the files and sequencially updates the names. It’s pretty fast considering it’s interpreted and the Async is good for not locking up the system.


 Create a new Apollo application in Flex Bulder, and past in this code into your main.mxml.

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:ApolloApplication xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”>
 <mx:Script>
  <![CDATA[
   import flash.filesystem.File;
   public var contents:Array = new Array();
   public var contents_filtered:Array = new Array();
   public function loadDirectory():void {
    filelist_txt.text = "";
    var directory:File = File.desktopDirectory.resolve(directoryName.text);
    contents = directory.listDirectory();
    var skipCount:uint = uint(fileCount.text);
    var ii:uint = 0;
    while (ii < contents.length) {
     filelist_txt.text += contents[ii].name+”,”+contents[ii].size+” bytes\n”;
       
     if (!contents[ii].isDirectory && !contents[ii].isHidden) {
      //trace(contents[ii].name,contents[ii].size,”bytes”);
      var jj:Number = ii/skipCount;
               var jjround:Number = Math.round(ii/skipCount);
              
               if (jj == jjround) {
                   contents_filtered.push(contents[ii]);
               }
     }
     ii++;
    }
   }
   
   public function processFiles():void {
    var ii:uint = 0;
    var destination:File = File.desktopDirectory.resolve(directoryName.text).resolve(“PROCESSED”);
    destination.createDirectory();
    messages_txt.text = “”; 
    while (ii < contents_filtered.length) {
     var newFileName:Array = fileName.text.split(“xxxx”);
        if (newFileName[0] == undefined) {
            newFileName[0] = “”;
        }
        if (newFileName[1] == undefined) {
            newFileName[1] = “”;
        }
       
        var chrCount:String = contents_filtered.length.toString();
           var chrDifStr:String = ii.toString();
           var chrDif:uint = chrCount.length – chrDifStr.length;
           var zeroFill:String = “”;
           for (var pp:uint = 0; pp < chrDif; pp++) {
              zeroFill += “0″;
           }
        trace (“chrCount:”+chrCount+” chrDifStr:”+chrDifStr+” chrDif:”+chrDif+” zeroFill:”+zeroFill);
          
        var newName:String = newFileName[0]+zeroFill+ii+newFileName[1]+”.”+fileExtension.text;
        var file2:File = destination.resolve(newName);
        if (file2.exists) {
         file2.deleteFile();
         file2 = destination.resolve(newName);
        }
     contents_filtered[ii].copyToAsync(file2);
     messages_txt.text += ii+” created:”+newName+”\n”;
     ii++;
    }
   }
  ]]>
 </mx:Script>
 
 <mx:TextInput y=”10″ width=”43″ text=”12″ id=”fileCount” right=”73″/>
 <mx:TextInput y=”10″ width=”43″ text=”aif” maxChars=”4″ id=”fileExtension” horizontalCenter=”63″/>
 <mx:TextInput y=”54″ width=”186″ text=”newFile_xxxx” id=”fileName” right=”21″/>
 <mx:Button label=”Process Files” click=”processFiles()” bottom=”10″ right=”21″/>
 <mx:Button label=”Load Directory Contents” click=”loadDirectory()” left=”10″ top=”11″/>
 <mx:TextArea x=”393″ width=”186″ id=”messages_txt” bottom=”40″ top=”84″/>
 <mx:TextArea x=”10″ width=”375″ id=”filelist_txt” bottom=”10″ top=”84″/>
 <mx:Label y=”12″ text=”Keep every ” right=”113″/>
 <mx:Label y=”12″ text=”filter by file extension:” horizontalCenter=”-32″/>
 <mx:Label y=”36″ text=”Directory name on the desktop:” left=”10″/>
 <mx:Label y=”36″ text=”New file name:” width=”97″ textAlign=”right” right=”21″/>
 <mx:Label y=”12″ text=”nth file” right=”21″/>
 <mx:TextInput y=”54″ width=”375″ text=”D471″ id=”directoryName” left=”10″/>
 
</mx:ApolloApplication>

I’m kind of happy that there’s some similarities to Visual Studio in Flex Builder — made the transition to it very comfortable.

About Bela Korcsog

Proud father of two children, happy husband to one wife. I've been programming various technologies and leading the development of huge projects for most of the last ten years. I've got some specific likes and dislikes through my experiences in the web site business but generally I'm pretty straightforward about it. Not a huge fan of the latest and greatest shiny toy (it took me four years to show an interest in Flash) I'm more than happy to code in any language that comes along (Actionscript is just so darn fun).
This entry was posted in AIR. Bookmark the permalink.

Comments are closed.