Using JSFL to change publish settings

August 6th, 2006 by Steven Sacks

The code in this post is old. Please refer to my updated version:

http://www.stevensacks.net/2008/03/26/using-jsfl-to-change-publish-profile-settings-v2/


Recently, I needed to automate generating templates and discovered that there was no direct way of editing the publish settings of the document using JSFL. The way to accomplish it is to export the publish profile (it's XML), read it back in as a string, swap out the node values, save the file, import the publish profile, and then delete the file. Here's how I accomplished it.

function setPublishSettings(fileName:String)
{
        var xml, from, to, snip;
        var fPath = getRelativePath() + "PublishProfile.xml";

        // export the profile and read it in
        fl.getDocumentDOM().exportPublishProfile(fPath);
        xml = FLfile.read(fPath);

        // replace the publish paths
        from = xml.indexOf("<flashFileName>");
        to = xml.indexOf("</flashFileName>");
        snip = xml.substring(from, to);
        xml = xml.split(snip).join("../deploy/" + fileName);

        var nodes = {};
        nodes.generatorFileName = "swt";
        nodes.projectorWinFileName = "exe";
        nodes.projectorMacFileName = "hqx";
        nodes.htmlFileName = "html";
        nodes.gifFileName = "gif";
        nodes.jpegFileName = "jpg";
        nodes.pngFileName = "png";
        nodes.qtFileName = "mov";
        nodes.rnwkFileName = "smil";

        for (var n in nodes) {
                from = xml.indexOf("<" + n + ">");
                to = xml.indexOf("");
                snip = xml.substring(from, to);
                xml = xml.split(snip).join("<" + n + ">" + fileName + "." + nodes[n]);
        }

        // write the file
        FLfile.write(fPath, xml);

        // import the altered profile
        fl.getDocumentDOM().importPublishProfile(fPath);

        // delete the altered profile
        FLfile.remove(fPath);

        // save flash file
        fPath = getRelativePath() + fileName + ".fla";
        fl.saveDocument(fl.getDocumentDOM(), fPath);
}

Posted in Flash, JSFL

20 Responses

  1. Chris Paterson

    Is there a way to do this so that it pulls the default.xml publish profile and writes a new one? That would be cool!

  2. Steven Sacks

    That's what line 6 does. It writes the default publish profile to the path you specify. Unless I'm misunderstanding your question?

  3. Gareth

    Flash MX 2004 and Flash 8 have a bug where the class path and AS version strings are not imported from the XML file that's created here. Are there any ways around this?

  4. Steven Sacks

    The way I get around that is to set up my initial FLA file with the proper class paths first and duplicate from that FLA. I haven't had an issue changing the publish version, though.

  5. Gareth

    Hmnm, that's fine if you know about the issue before you start, but I've just been given 100s of FLA files that were developed months/years ago to change. I spent an hour or two making a JSFL file that recursively searches through folders and applies changes to all the FLA files it finds, only to find that AS version and class path are lost due to a stupid bug! =/

    Macromedia really sucks HARD sometimes.

    Thanks for the help and the quick response.

  6. Steven Sacks

    Thankfully, this bug has been fixed in Flash CS3, and I used this code in the Gaia framework for Adobe Flash.

  7. Andy

    Oh, and did not know about it. Thanks for the information …

  8. mh

    Hi,
    Very new to jsfl and i tried your script . However it failed as getRelativePath() wasnt recognised, i guess then thats a function yoiu declare somewhere else and not a native jsfl method?

  9. Steven Sacks

    getRelativePath() simply uses the native JSFL property "fl.getDocumentDOM().path". It's in the JSFL documentation under "Document object".

    "document.path returns the path of the document in a platform-specific format. If the document has never been saved, this property is undefined."

    Check out the documentation for JSFL to read more about using the "document.path" property to get the relative path of the open document. Another option is to use the fl.browseForFolderURL() method to open a dialog to find the relative path.

    Hope that helps! JSFL is an awesome way to automate tasks in Flash and save you time. It's definitely worth learning.

  10. TJ

    I would love to learn a little more about your function here. Do you have a full JSFL script which uses this function and has the supporting getRelativePath() function as well you could post?

    Also, the "< - -" characters in your code are screwing up the display of your site ;)

  11. Steven Sacks

    My Gaia Framework uses something similar to this in the JSFL that scaffolds pages. Install it and open the JSFL files it uses to see how it works.

  12. Quince

    Do you know where there is a list of the allowable xml tags in a xml profile?

  13. Steven Sacks

    Go into Flash, export a publish profile manually and look at the XML file it creates. :)

  14. Quince

    Thanks, I already did that, but I want to know what is actually allowed in the xml. you can also see that their are profiles for video encoding, flash lite, and projects. but is there any documentation on the publish xml file? attributes and tags…

  15. Steven Sacks

    Not that I know of. If you ask on Flashcoders or Flash_Tiger, you might get an answer from Adobe's technical writer Francis Cheng.

  16. Travis Nelson

    What is supposed to happen in the replace publish paths section? When I used the code (on a mac) the new publish path is put between every single character. Where in the xml file are the publish path changes supposed to go?

    What is confusing me is the code:

    xml.indexOf("");

    What is that searching for?

  17. Steven Sacks

    The indexOf("") is WordPress stripping out characters it shouldn't be. I'll fix it.

  18. Barry

    Hi Steve, thanks for your post, it's been a massive help, although i still can't quite believe that you can't access and edit publish settings directly through jsfl???

    I was also wondering if you might be able to point me in the right direction with something else i'm stuck on. I've written a jsfl script that recurses through a directory structure, creates a new file for each .flv file that it finds, imports the flv (to embed it) and then outputs a swf. The only problem is that i can't find a way to automate the 'import video' dialog box that pops up when you import an flv into the fla? I've got to do this for 850 flv's so i'm hoping you might know a way to communicate with this dialog box through jsfl? Otherwise i'm looking at a serious RSI!

    I noticed an earlier comment from Quince mentioning profiles for video encoding and have got my hopes up that this may be the ray of light of was looking for! Any ideas?

    cheers.

  19. Toy

    Hi, thanks for posting, it seems quite useful. When I run the command in flash tho, I get an error saying

    Line 141 :A was expected.

    Any idea ???

  20. Toy

    Sorry about that, it removed a part of my post. Error is PublishProfileProperties element was expected

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

About Steven Sacks

I am a professional Flash developer with over 13 years of programming experience. I have consulted for high-profile agencies and companies in San Francisco, Los Angeles, Atlanta and New York, and developed numerous award-winning websites and rich internet applications for clients including Adobe, Fox Sports, FX Networks, Anheuser-Busch, GE, DirecTV, ESPN, The Weather Channel, Home Depot, and Coca-Cola.

I am the author of the open-source Gaia Framework for Adobe Flash, which dramatically reduces development time and makes developing Flash sites much easier.