navigateToURL() on CD-ROM With Flash Projector and FireFox on Mac

Posted by
Ziwei

Posted On
Dec 24 2008 12:59 am

While creating a projector file meant for a cd-rom, i ran into a peculiar bug that costs me a couple of sleepless nights. Just like how i usually author a swf file meant for web-output, i used navigateToURL() to call a few html files saved locally on the cd. But when the html files are called the parent path of the cd-rom is dropped. Safari is still able to read the url and open the correct html file with or without the parent path but Firefox is completely lost without the parent path.

file:///Volumes/resources/folder/index.html
becomes
file:///resources/folder/index.html
when the parent path is dropped

Using loaderInfo.url() does not help either as the absolute url of the projector returned is missing the parent path too. However after giving it some thoughts, it appears that i am still able to use loaderInfo.url() to solve my problem.

The solution? By simply replacing “file///” with “file///Volumes/” using a string replace function, i am able to get the path that Firefox is able to read. Problem solved. But what if the user is using windows? The above method would be invalid as windows drive path does not include “Volumes”. However, the fix to this problem is simple, we simply run a function to check which platform user is running and trigger the replace function accordingly.

  1. var absoluteDir = stage.loaderInfo.url;
  2. // Checking to see if user is running a macintosh or windows platform.
  3. var system:String;
  4. getSystemPlatform();
  5. function getSystemPlatform():void {
  6. if (Capabilities.os.substr(0, 3) == "Win") {
  7. system = "Win";
  8. } else if (Capabilities.os.substr(0, 3) == "Mac") {
  9. system = "Mac";
  10. }
  11. }
  12. //Create absolute path with parent path "Volumes" if user is running on a mac platform
  13. function buildLink(targetURL):String {
  14. var poslastslash:Number = targetURL.lastIndexOf("\\");
  15. if (poslastslash == -1) {
  16. poslastslash = targetURL.lastIndexOf("/");
  17. }
  18. var folderurl:String = targetURL.substr(0, poslastslash+1);
  19. var poscolon:Number = folderurl.indexOf("|");
  20. if (poscolon<-1 || poscolon>-1 ) {
  21. var folderurlstart:String = folderurl.substr(0, poscolon);
  22. var folderurlend:String = folderurl.substr(poscolon+1);
  23. folderurl = folderurlstart+":"+folderurlend;
  24. }
  25. var myPattern:RegExp = /file:\/\/\//i;
  26. folderurl = folderurl.replace(myPattern, "file:///Volumes/");
  27. return folderurl;
  28. }
  29. //get absolute parent directory
  30. getParentDir();
  31. function getParentDir():void {
  32. if (system == "Mac") {
  33. absoluteDir = buildLink(absoluteDir);
  34. } else if (system == "Win") {
  35. // we don't have to do anything if user is running a window platform
  36. }
  37. }

To get the request url of the html file, just combine the filename and folder name of the html file relative to the projector file and append it to the absoluteDir in the script above before sending it to navigateToURL().

Part of my solution came from here.

ยง

Comments ( 9 )

  1. March 21st, 09 3:23 am Jack Pitzer

    Greetings,
    I have a question about the issue you discussed in your post.
    I’m also creating a cross platform CD ROM, which basically is an interface with 320 links to files for the user to download.
    It started out as windows only, so I was going to use SWF Studio, which provided a great solution. Clicking on the link brings up a “Save file as” dialog box, no browser or anything else.
    But then, it became a cross platform thing, and I needed to go back to the getURL method.
    It’s not as elegant because of using a browser to help with the download.
    So, here’s my question. If I were to use your code, where do I place it?
    Does it go with the buttons, or somewhere else?

  2. March 22nd, 09 2:12 pm Ziwei

    Jack
    You can place the code in your document class or you can place the buildLink function in a class and call it whenever you need to resolve a url. Just remember you need to identity the system (mac or window) that the user is using first.

  3. March 24th, 09 11:39 pm Bob Clagett

    thanks for this man, you made today much easier for me :)

  4. April 1st, 09 2:39 am Matthew Shettler

    Hi Ziwei,
    Thanks so much for putting this up here.

    I guess I’m a bit slower than Jack, because I don’t quite get how to correctly apply the code. Which instance of absoluteDir do I append the relative URL to? and how do I append it to ten different links in such a way that URLRequest() and navigateToURL() will receive it?

    Could you provide an example?

  5. April 1st, 09 2:47 am john

    Great article, I was about to start pulling hair before I found this.

    Question: I’m using tags pulled from an XML document to link out to files on my CD-ROM. Is there anyway to get to work for that?

    Thanks!

  6. April 1st, 09 11:55 pm Ziwei

    Matthew Shettler
    There’s only one absoluteDir instance and that instance is your stage.loaderInfo.url minus your swf file name. For instance, if your swf file is file:///resources/main.swf, the absoluteDir will return file:///Volumes/resources/ after calling the getParentDir() function with the original stage.loaderInfo.url value. You only need to run the script once at the start of your movie to get the absolute directory path of your movie file and store it under the absoluteDir variable.

    To apply it to a URLRequest you can try the following;

    var fileLink:string = ‘mylink.html’
    var requestLink= absoluteDir + fileLink;
    var url:URLRequest = new URLRequest(requestLink);
    var variables:URLVariables = new URLVariables();
    navigateToURL(url,”_self”);

    The fileLink in the example above is the relative url of 1 of your link. Here we add it together with absoluteDir from the script in the main article in a new variable called requestLink before we finally send requestLink to a new URLRequest() which will open up the file we want.

  7. April 2nd, 09 12:00 am Ziwei

    john
    I don’t know how you format your XML document, but you can try using a loop to append the absoluteDir to the tags and push them into another array variable in flash. You can then use the new formatted url in the array for your URLRequest().

  8. April 2nd, 09 12:35 am Matthew Shettler

    Thanks! That worked like a charm.

  9. August 23rd, 09 5:48 pm mike

    can someone do this in AS2?

Post Comment

Remember! Spamming is for losers

Trackback from your own website