Flex: Get self SWF file name?
Asked Answered
B

5

6

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

Thanks!

Buitenzorg answered 22/12, 2009 at 20:49 Comment(0)
B
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);

Blest answered 22/12, 2009 at 21:0 Comment(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.Buitenzorg
Yeah, doesn't look like stage is ready until the FlexEvent.APPLICATION_COMPLETE event is dispatched.Blest
S
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;
}
Skaggs answered 15/1, 2012 at 9:36 Comment(0)
M
1

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

Mucoprotein answered 22/12, 2009 at 20:51 Comment(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.Buitenzorg
A
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);
 }
}
Aramanta answered 22/12, 2009 at 21:0 Comment(0)
F
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.

Fanfare answered 15/6, 2012 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.