6

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

Thanks!

5 Answers 5

9

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);

2
  • 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.
    – JD Isaacks
    Commented Dec 22, 2009 at 21:11
  • Yeah, doesn't look like stage is ready until the FlexEvent.APPLICATION_COMPLETE event is dispatched. Commented Dec 22, 2009 at 21:18
3

Just a helpful note: If you load one SWF into another, the loaded (inner) SWF will return an erroneous result if you use loaderInfo.url to try to get the filename. For instance, something like:

Path/To/Outer.swf/[[DYNAMIC]]/1

Instead of:

Path/To/Inner.swf

Beware!

That said, here is the code I use to get the current SWF name:

function SWFName(symbol:DisplayObject):String
{
    var swfName:String;
    swfName = symbol.loaderInfo.url;
    swfName = swfName.slice(swfName.lastIndexOf("/") + 1); // Extract the filename from the url
    swfName = swfName.slice(0, -4); // Remove the ".swf" file extension
    swfName = new URLVariables("path=" + swfName).path; // this is a hack to decode URL-encoded values
    return swfName;
}
1

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

1
  • 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.
    – JD Isaacks
    Commented Dec 22, 2009 at 20:57
1

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);
 }
}
1

Things have changed a bit in more recent versions so I'll give an answer for Adobe Flash Builder 4.6 (geared towards Flash in browser, but you get the idea).

<s:Application ... applicationComplete="alertSwfUrl()">

<fx:Script>
<![CDATA[
    import mx.core.FlexGlobals;

    private function alertSwfUrl():void {
        var a:LoaderInfo = FlexGlobals.topLevelApplication.stage.loaderInfo;
        ExternalInterface.call('alert', a.url); 
    }

]]>
</fx:Script>

</s:Application

Check out the LoaderInfo docs to figure out how to use the loaderInfo object associated with the stage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.