Using JSFL To Change Publish Profile Settings v2
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
March 26th, 2008 at 2:49 pm
[...] http://www.stevensacks.net/2008/03/26/using-jsfl-to-change-publish-profile-settings-v2/ [...]
March 26th, 2008 at 8:56 pm
Thanks for the update.
March 27th, 2008 at 2:19 am
In CS3 you can use E4X in JSFL also
March 27th, 2008 at 2:06 pm
True, but this code has to work in Flash 8, as well.
April 9th, 2008 at 3:40 pm
interesting! could you post a link to gaia's scaffold, that you mention? i cant seem to find it in google
April 22nd, 2008 at 7:25 am
Is it possbile to edit a small piece of the publish profile and export it?