Flex: get your own SWF file name?

Is it possible to programmatically determine the name of the .swf file in which my class works?

Thanks!

+6
flex filenames actionscript-3 flex3
source share
5 answers

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

trace (stage.loaderInfo.url);

+8
source share

Just useful to note: if you load one SWF into another, the loaded (internal) SWF will return an erroneous result if you use loaderInfo.url to try to get the file name. For example, something like:

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

Instead:

Path/To/Inner.swf

Caution!

So, here is the code that 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; } 
+3
source share

Not from the inside, afayk. What do you need it for? Perhaps this is the best way to do this.

+1
source share

You can use loaderInfo.loaderURL to get the full path and name of your SWF

Class Example:

 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
source share

In later versions, the situation has changed a bit, so I will give an answer for Adobe Flash Builder 4.6 (oriented to Flash in the 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 

Browse the LoaderInfo Docs to find out how to use the loaderInfo object associated with the stage .

+1
source share

All Articles