24
Feb
09

Actionscript SWF path utility

This utility will ensure that any file pointers you use in your actionscript are always relative to the location of your embedded swf not the location of the page that embeds your swf.

To use, we instantiate the class and call the getPath() method on the new instance, which takes the file source path as a parameter:

var mySitePath:SitePath = new SitePath();
mySitePath.getPath(’../../someFile.jpg’);

This will return the location of the file relative to your swf, no matter where it is embedded.

package utilities {
  import flash.text.*;
  import flash.display.*;
  import flash.net.*;

  public class SitePath extends MovieClip {
    var loader:Loader;
    var info:LoaderInfo;
    public var currentPath:String;
    public var parentPath:String;
    public function SitePath(){
      init();
    }
    public function init(){
      loader = new Loader();
      info = LoaderInfo(loader.contentLoaderInfo);
      createDirPath();
    }

    function createDirPath(){
      var fullPath:String = info.loaderURL;
      var pathArray:Array = fullPath.split('/');
      setCurrentPath(pathArray);
      setParentPath(pathArray);
    }

    function setCurrentPath(anArray:Array){
      anArray.pop();
        currentPath = anArray.join('/');
    }
    function setParentPath(anArray:Array){
      anArray.pop();
      parentPath = anArray.join('/');
    }
    public function getPath(directory:String):String{
      trace('getPath');
      var pathArray:Array = directory.split('/');
      var newPath:String;
      //If directory is passed in as full path
      if(directory.match('://')){
        newPath = directory;
      }
      //If directory is passed in as absolute path
      else if (directory.substr(0, 1)=='/'){
        //Check to see if files exist locally
         if(currentPath.match('file://')){
          newPath = 'file://'+directory;
        }
        //Check to see if files exist on webserver
        if(currentPath.match('http://')){
          var localDomainLC:LocalConnection = new LocalConnection();
          var myDomainName = localDomainLC.domain;
          newPath = 'http://'+myDomainName+directory;
      }
      }
      //If directory is passed in as parent directory some combination of dot-dot-slash
      else if(pathArray[0] == '..'){
        //Return correct directory based on number of dot-dots
        newPath = replaceDotDots(directory);
      }
      //If directory is passed as current directory dot-slash
      else if (pathArray[0] == '.'){
        pathArray[0] = currentPath;
        newPath = pathArray.join('/');
      }
      //If directory is passed in as current directory
      else{
        newPath = currentPath+'/'+pathArray.join('/');
      }
      return newPath;
    }
    //Walk through path containing multiple dot-dots and return correct path
    function replaceDotDots(parentDirectory:String):String{
      var currentPathArray:Array = currentPath.split('/');
      var myArray:Array = parentDirectory.split('/');
      var i:int = 0;
      while (myArray[i]=='..'){
        currentPathArray.pop();
        i++;
      }
      myArray.splice(0, i);
      var fullPath:String = currentPathArray.join('/')+'/'+myArray.join('/');
      return fullPath;
    }
  }
}


2 Responses to “Actionscript SWF path utility”


  1. 1 Ted Stevens Feb 24th, 2009 at 2:58 pm

    Thanks for posting! I’ve noticed this problem for quite some time and never really found much documentation on it.
    Very elegant solution.

  2. 2 Moneyplay Sep 23rd, 2011 at 8:12 pm

    excelente lisse de psiptamo y acove con iuguiria ogurbem. tindio a quistina y bianaci mizand con tissentre condo!

Leave a Reply