Use JSFL to change Publish Settings in Flash

It's been a while since I’ve touched on JSFL, but recently I found myself diving into it again for a project at work and decided to share a method for updating the publish settings of an .fla file using JSFL. When I initially set out to do this, I wasn't sure where to begin and after a bit of googling, I found a lot of others who had run into the same problem, but a simple solution wasn't readily available.

The most popular article I found on the subject was this one from Steven Sacks. It looked a bit overly complicated for what I needed and is quite old (2008). I kept looking and eventually found my solution by modifying an example from the Adobe docs.

First, let's look at what a publishing profile actually is because perhaps some out there don't really know. Don't feel bad; unless you find yourself in a position where its necessary to create some kind of template you would never really need to work with them.

A publishing profile is simply an xml file that tells Flash how to publish an fla. Flash has a built-in profile named Default. You can see this if you open up the publish settings in the Flash IDE (ctrl+shift+F12). Then look at the top and you should see a drop down menu labeled "Profile" with the text "Default" already selected. Below is an image from CS5.5 which demonstrates this.

Let's take a look at the file to see what we may want to change. You can grab the file from your Publish Profiles folder in the Flash config directory. It is located here on my Windows 7 box (change USER_NAME to your user name):

C:\Users\USER_NAME\AppData\Local\Adobe\Flash CS5.5\en_US\Configuration\Publish Profiles

Alternatively, you can click the icon next to the drop down and select "Import profile..." which opens up right to the correct directory. Then just copy the Default.xml to your desktop or somewhere more accessible and cancel the import.

Open the file in your xml editor of choice. You'll see that it is a fairly lengthy file. A few of the nodes that will be of interest to us include the one on line 25; flashFileName, line 4; defaultNames, and line 15; flashDefaultName.

Now that we know what the publishing profile looks like, let's see how we can modify it using JSFL. First we'll create a new jsfl file and make a few simple changes to set up our template.

var doc = fl.getDocumentDOM();

doc.width = 640;
doc.height = 480;
doc.frameRate = 24;

All we've done above is grab a reference to the current fla file and set a few properties. Next, we'll snag a copy of the default publishing profile like so:

var profileXML = doc.exportPublishProfileString('Default');

The variable profileXML (a string) now contains all the text in the Default.xml publishing profile. The next step is to modify that string to our liking.

// remove html publishing and default naming
profileXML = profileXML.replace("<html>1</html>", 
                                  "<html>0</html>");

profileXML=profileXML.replace("<defaultNames>1</defaultNames>", 
                    "<defaultNames>0</defaultNames>");

profileXML=profileXML.replace(
                  "<flashDefaultName>1</flashDefaultName>",           
                  "<flashDefaultName>0</flashDefaultName>");

We start off with a few calls to the replace method to turn off html publishing (I hate that), default names and the flash default names. I'm not really sure what the difference is on the last two, but simply changing the flashDefaultName node is not enough. We turn them off by replacing the 1 with a 0. Also, note that while we're targeting a few select nodes in this tutorial, you can use this replace technique to change any node you wish.

Pretty simple so far, right? Good, because now we're ready to change the publishing path of the .fla. To do so, we first want to find the current name of the document in the publishing profile. The node we're looking for is called <flashFileName>, so let's find the beginning and end tag for that node.

var findString = "";
var startIndex = profileXML.indexOf(findString) + findString.length;

findString = "";
var endIndex = profileXML.indexOf(findString);

var curName = profileXML.substring(startIndex, endIndex);

As you can see, first we find the opening tag. Then we add the tag's length to our position; this gives us the starting index of the node's content. Next we find the index of the closing tag. Lastly, we use our indices to extract the current file name from the xml. Now you can do a replace call and fill in the publishing name with whatever you want.

For instance, suppose you have a project directory and in the directory are two folders: source and bin. The source folder is where your fla is at, but you want it to publish to the bin directory. In this case, you could set up a publishing path like this:

var pubPath = "../bin/" + doc.name;
profileXML = profileXML.replace(curName, pubPath);

With the publishing path now set, all that's left to do is overwrite the current publishing profile's text with our updated profileXML variable. We do that with a call to importPublishProfileString like this:

fl.trace("Will publish to: " + pubPath + ".swf");
doc.importPublishProfileString(profileXML);

Notice we also added a trace to tell the user where the file is going to publish. Adding this little message can help avoid confusion.

With the jsfl file complete, save it to the Commands folder inside the flash config directory. I named mine Create Template. So to test it out, I'll open up a new ActionScript 3.0 fla in Flash, save it with the name I want (MyTest) and then select Commands >> Create Template from the Flash menu. The publishing results are shown below.

Before using the Create Template command:

After using the Create Template command:

One thing to note: it is a good idea to save the file first; otherwise the publish name will not match the name of the file (unless you run the command again). Also, while this tutorial was made for CS 5.5, this will also work with CS 5. I have not tested it on any others, but it may work on them as well.

That's it for this tutorial; I hope it comes in handy! As usual, you can download the source files below; enjoy!

Download Source

Bookmark and Share

7 Responses to “Use JSFL to change Publish Settings in Flash”

  1. JG says:

    I know this is a couple months later, but thank you for this! I had found the other article you mentioned from 2008 but it wasn’t working for me.

    One very tiny thing I ran into was the “.fla” being used for the filenames on all my publish paths. I solved that by changing this:
    var pubPath = “../bin/” + doc.name;
    to this:
    var pubPath = “../bin/” + doc.name.replace(“.fla”,””);

    You are wonderful for posting this. I no longer have to manually enter all my relative publish paths!

  2. rossisen says:

    Thank you for the helpful tutorial. I think that for CS4/AS2, you need to camel case . I used this together with the code from the Publish Files extension to publish all files in directory to a new place (and add an empty movieclip along the way)

  3. Rhuno says:

    No worries; it’s always great to get feedback! Thanks for visiting the site and I’m glad you found it helpful!

  4. prodaea says:

    found this while trying to figure out an ant script i’m working on. Long story short, while I really liked this implementation it didn’t work for me for nested or generic nodes like swc-path. While it would probably be easier to build my own profile xml, you can use E4X in the jsfl because Flash uses Mozilla’s SpiderMonkey engine. So you should be able to simply do something like:
    [code]
    var theProfile = new XML(doc.exportPublishProfileString('Default'));
    theProfile.PublishFormatProperties.flashFileName = "../bin/" + doc.name;
    doc.importPublishProfileString(theProfile.toString());
    [/code]

    I haven’t fully tested the .toString() part, that’s just an assumption at this point. Hope this helps!

  5. Jed says:

    Confirming that the E4X approach works. It’s a much more robust approach than doing find/replace in the raw XML string IMO. theProfile.toXMLString() might be better for the final step.

  6. sonicoliver says:

    Brilliant! Thank-you!

  7. sonicoliver says:

    Oh yeah, they’ve since updated the xml, the file name node is camel case… so should be

    var findString = “”;

    Otherwise it will return -1, then fail to importPublishProfileString

    Thanks again…

Leave a Reply

Subscribe to RSS feed FGS5 Badge