views:

558

answers:

3

Is there a way I can programmatically determine the filename of the .swf my class is running in?

Thanks!

A: 

Not from within flash, afaik. What do you need it for? There might be a better way to do it.

Jonatan Hedborg
Well I am building a custom Logger class, That will send the logs to a script to be saved. I am making it so I can reuse this class with other projects. So I want the class to be able to auto-detect what project it is running in, so it can log that as well.
John Isaacks
A: 

You can use loaderInfo.loaderURL to get the full path and name of you swf

Example of a class:

public class Main extends Sprite {
 private function init():void {
  removeEventListener(Event.COMPLETE, init);
  var myUrl:String=loaderInfo.loaderURL;
  var tmp:Array=myUrl.split("/");
  var myName:String=tmp[tmp.length-1].split(".swf")[0];
 }

 public function Main() {
  super();
  if (stage)
    init();
  else
    addEventListener(Event.COMPLETE, init, false, 0, true);
 }
}
Patrick
+1  A: 

Stage has a loaderInfo property, which contains a url property that has the information you're looking for. You can get the stage property from any DisplayObject in Flex.

trace(stage.loaderInfo.url);

RJ Regenold
Thanks.. Also I can use Application.application.stage.loaderInfo.url to get it from a NON DisplayObject, also it gives an Error if you try to access it before Application.CreationComplete.
John Isaacks
Yeah, doesn't look like stage is ready until the FlexEvent.APPLICATION_COMPLETE event is dispatched.
RJ Regenold