Using JSFL To Change Publish Profile Settings v2

March 26th, 2008 by Steven Sacks

I have some improved code for changing the publish profile settings of a Flash file using JSFL, which works with AS2 and AS3. This is a slight alteration of the code in Gaia's JSFL scaffolding, so if you want to see a more robust version, check out Gaia's scaffolding JSFL.

// fileURI is the full path to the Flash file
// asVersion is 2 or 3
function setPublishProfileSettings(fileURI, asVersion)
{
        // does flash file exist
        if (fl.fileExists(fileURI)
        {
                var xml, from, to, delta;

                // open the flash file
                fl.openDocument(fileURI);

                // separate the file name from its folder path
                var fileName = fileURI.split("/").pop();
                var folderPath = fileURI.split(fileName)[0].join("/");
                fileName = fileName.split(".")[0];

                // export the profile and read it in
                var pPath = folderPath + "/_Profile_.xml";
                fl.getDocumentDOM().exportPublishProfile(pPath);
                xml = FLfile.read(pPath);

                // override default names to 0
                from = xml.indexOf("<defaultNames>");
                to = xml.indexOf("</defaultNames>");
                delta = xml.substring(from, to);
                xml = xml.split(delta).join("<defaultNames>0");

                // override flash default name to 0
                from = xml.indexOf("<flashDefaultName>");
                to = xml.indexOf("</flashDefaultName>");
                delta = xml.substring(from, to);
                xml = xml.split(delta).join("<flashDefaultName>0");

                // replace the publish path for swf
                from = xml.indexOf("<flashFileName>");
                to = xml.indexOf("</flashFileName>");
                delta = xml.substring(from, to);
                var parentPath = "../";
                if (fileName.indexOf("/") > -1)
                {
                        var splitPath = fileName.split("/");
                        splitPath.length--;
                        var i = splitPath.length;
                        while (i--)
                        {
                                parentPath += "../";
                        }
                }
                xml = xml.split(delta).join("<flashFileName>" + parentPath + "deploy/" + fileName + ".swf");

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

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

                // make sure package paths look in ./classes, and classes export in frame 1
                from = xml.indexOf("<ActionScriptVersion>");
                to = xml.indexOf("</ActionScriptVersion>");
                delta = xml.substring(from, to);
                xml = xml.split(delta).join("<ActionScriptVersion>" + asVersion);

                from = xml.indexOf("<PackageExportFrame>");
                to = xml.indexOf("</PackageExportFrame>");
                delta = xml.substring(from, to);
                xml = xml.split(delta).join("<PackageExportFrame>1");

                // set package paths based on AS version
                if (asVersion == 2)
                {
                        from = xml.indexOf("<PackagePaths>");
                        to = xml.indexOf("</PackagePaths>");
                }
                else
                {
                        from = xml.indexOf("<AS3PackagePaths>");
                        to = xml.indexOf("</AS3PackagePaths>");
                }
                delta = xml.substring(from, to);
                var classPath = "./";
                if (fileName.indexOf("/") > -1)
                {
                        classPath = "";
                        var splitPath = fileName.split("/");
                        splitPath.length--;
                        var i = splitPath.length;
                        while (i--)
                        {
                                classPath += "../";
                        }
                }
                if (asVersion == 2)
                {
                        xml = xml.split(delta).join("<PackagePaths>" + classPath + "classes");
                }
                else
                {
                        xml = xml.split(delta).join("<AS3PackagePaths>" + classPath + "classes");
                }
                // write the modified profile and import it
                FLfile.write(pPath, xml);
                fl.getDocumentDOM().importPublishProfile(pPath);

                // save and publish the fla
                fl.saveDocument(fl.getDocumentDOM(), fullFilePath);
                fl.getDocumentDOM().publish();

                // delete the publish profile xml (no longer needed)
                FLfile.remove(fPath);
        }
}

Posted in JSFL, Tips/Tricks

6 Responses

  1. Using JSFL to change publish settings | flash developer | steven sacks

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

  2. Travis Nelson

    Thanks for the update.

  3. maliboo

    In CS3 you can use E4X in JSFL also :)

  4. Steven Sacks

    True, but this code has to work in Flash 8, as well. :)

  5. Zack

    interesting! could you post a link to gaia's scaffold, that you mention? i cant seem to find it in google ;)

  6. Steve

    Is it possbile to edit a small piece of the publish profile and export it?

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.