My solution for batch replacing fonts in FLA file
Quote:
Originally Posted by McBeaver
(Post 622595)
I have to edit someone else's fla files. There are a bunch of them, mostly timeline animations, not much actionscript. They were done on a mac, so I need to change all the font links to new PC chosen ones, not just remap. I've been experimenting with a JSBL script to do this....
|
Hi, I have the almost same goal - batch replacement of MANY (12) fonts in the numerous FLA files. Happily, I've found your post so had a good starting point to write my own script. Finally I've got a JSFL script which not only replaces fonts but also constructs and saves a report about each found and each replaced item for each file, also does found fonts summary etc. Script goes recursively through the whole document until find all text item. For each text item its path is constructed too to make array of only text items of the document for teh future usage (a bit buggy now, maybe someone will help me to fix it). Than you for helping me with the start.
Then, I wrote second JSFL file for batching. It runs the first one over the multiple FLA files (there's near 100 source files in my case)
Here's these first file, which does the replacement and saves the report:
1.
"C:\Documents and Settings\rost\Local Settings\Application Data\Macromedia\Flash 8\en\Configuration\Commands\[FR] Replace Fonts.jsfl"
Code:
var dom = fl.getDocumentDOM();
dom.editScene(0);
//dom.selection = dom.getTimeline().layers[0].frames[0].elements;
fl.outputPanel.clear();
fl.trace("********************************************************************");
fl.trace(" START OF FONT REPLACEMENT REPORT\n");
fl.trace(" FONT REPLACEMENT UTILITY");
fl.trace(" v 1.0.2");
fl.trace(" Author: Rostislav Siryk");
fl.trace(" http://flash-ripper.com/en/");
fl.trace("********************************************************************");
fl.trace("\nFILE NAME:\t" + dom.name + ".fla");
fl.trace("FILE PATH:\t" + dom.path + "\n");
//fl.saveDocumentAs(.documents[0].path);
// Font to be replaced
var font_select = new Array("MyLifeSans",
"MyLifeSer",
"Myriad",
"Myriad-Bold",
"Myriad-Roman",
"Frutiger 45 Light",
"Frutiger 55 Roman",
"Garamond Book",
"ArialNarrow-Bold",
"Avenir LT Std 65 Medium",
"Helvetica",
"Verdana",
"Arial Black",
"Arial Narrow",
"Myriad Roman"
);
var textElements = new Array();
var numTotalRuns = 0;
var strOutput = "";
var fontsFound = new Array();
var strFoundFonts = "";
var numTextItemsTotal = 0;
var numTextItemsReplaced = 0;
var strLayerName = "";
var strItemFullPath = "";
var strItemHumanPath = "";
// Delimiters used to create element paths
var strTimelinesDelimiter = ".libraryItem.timeline";
var strLayerDelimiter = ".layers";
var strFrameDelimiter = ".frames";
var strElementDelimiter = ".elements";
var BR_O = "[";
var BR_C = "]";
findTextElements = function(theTimeline)
{
if(theTimeline && theTimeline.layers.length > 0)
{
var theLayers = theTimeline.layers;
for(var numLayer = 0; numLayer < theLayers.length; numLayer++)
{
// Parse FRAMES of the LAYER
var theFrames = theLayers[numLayer].frames;
for(var numFrame = 0; numFrame < theFrames.length; numFrame++)
{
// Parse only KEY Frames. I said PARSE ONLY \KEY/ FRAMES.
if (numFrame == theFrames[numFrame].startFrame)
{
// Parse ELEMENTS of the KEYFRAME
var theElements = theFrames[numFrame].elements;
for(var numElement=0; numElement < theElements.length; numElement++)
{
numTotalRuns ++;
var foundElement = theElements[numElement];
// if element is not text but instance
if(foundElement.elementType == "instance")
{
// Add LAYER to the item full path
strItemFullPath += strLayerDelimiter + BR_O + numLayer + BR_C;
strLayerName = "." + theLayers[numLayer].name;
strItemHumanPath += strLayerName;
// Add FRAME, ELEMENT and TIMELINE to the item full path
strItemFullPath += strFrameDelimiter + BR_O + numFrame + BR_C + strElementDelimiter + BR_O + numElement + BR_C + strTimelinesDelimiter;
strItemHumanPath += "[fr" + numFrame + "]" + (foundElement.name || "(unnamed)");
findTextElements(foundElement.libraryItem.timeline);
}
else if (foundElement.elementType == "text")
{
// Add LAYER to the item full path
strItemFullPath += strLayerDelimiter + BR_O + numLayer + BR_C;
strLayerName = "." + theLayers[numLayer].name;
strItemHumanPath += strLayerName;
// Add FRAME and ELEMENT to the full path and FINALIZE path
strItemFullPath += strFrameDelimiter + BR_O + numFrame + BR_C + strElementDelimiter + BR_O + numElement + BR_C;
strItemHumanPath += "[fr" + numFrame + "]" + (foundElement.name || "(unnamed)");
textElements.push({theElement: foundElement,
itemID: numTextItemsTotal,
itemPath: strItemFullPath,
itemHumanPath: strItemHumanPath
});
// Reset the paths for the next iteration
strItemFullPath = "";//strLayerDelimiter + BR_O + numLayer + BR_C;
strItemHumanPath = "";
numTextItemsTotal++;
/* numElement = theElements.length;
numFrame = theFrames.length;
numLayer = theLayers.length;
break;*/
}
}
}
else
{
// NO KEYFRAME ACTION
}
}
}
}
}
/*for(var i=0; i < dom.library.items.length; i++)
{
fl.trace("LIBITITEM.");
findTextElements(dom.library.items[i].timeline)
}*/
function enumerateAllTextElements()
{
for(var numTe = 0; numTe < textElements.length; numTe++)
{
var te = textElements[numTe];
strOutput += "\n\tTEXT #" + te.itemID
+ "\n\tFont:\t" + te.theElement.getTextAttr("face")
+ "\n\tPath:\t" + te.itemPath
//+ "\n\tSeek:\t" + te.itemHumanPath
+ "\n\tText:\t" + te.theElement.getTextString()
+ "\n";
}
}
function replaceAllFonts()
{
for(i=0; i<font_select.length; i++)
{
font_replaced = font_select[i];
font_replacement = "Arial";
if(font_replaced == "Garamond Book")
{
font_replacement = "Times New Roman";
}
for(var te = 0; te < textElements.length; te++)
{
replaceFont(textElements[te]);
}
}
}
function replaceFont(objText)
{
var textElement = objText.theElement;
//fl.trace("TE= " + textElement.theElement);
if(textElement.getTextAttr("face"))
{
var oldFont = textElement.getTextAttr("face");
var oldSize = textElement.getTextAttr("size");
var txt = textElement.getTextString();
addFoundFont(oldFont);
if(oldFont == font_replaced)
{
numTextItemsReplaced++;
textElement.setTextAttr("face", font_replacement);
var strSizeChange = "";
if(!isNaN(oldSize) && (oldFont == "FullLifeSans" || oldFont == "FullLifeSer"))
{
newSize = oldSize * 0.85 || Math.floor(oldSize * 0.85);
textElement.setTextAttr("size", newSize);
strSizeChange =
"\n\tOLD Size:\t" + oldSize
+ "\n\tNEW Size:\t" + newSize;
}
strOutput += "\n\tREPLACE #" + objText.itemID
+ "\n\tOLD Font:\t" + oldFont
+ "\n\tNEW Font:\t" + font_replacement
+ strSizeChange
+ "\n\tPath:\t" + objText.itemPath
//+ "\n\tSeek:\t" + objText.itemHumanPath
+ "\n\tText:\t" + txt
+ "\n"
}
else
{
//strOutput += "\t[NO CHANGES TO FONT]\n";
}
}
}
function addFoundFont(fontName)
{
isInFontsFoundAlready = false;
for(var i=0; i<fontsFound.length; i++)
{
if(fontsFound[i] == fontName)
{
isInFontsFoundAlready = true;
break;
}
}
if(!isInFontsFoundAlready)
{
fontsFound.push(fontName);
strFoundFonts += "\n\t" + fontsFound.length + "\t" + fontName;
}
}
/*font_replaced = "FullLifeSer";
font_replacement = "Arial";*/
findTextElements(dom.getTimeline());
strOutput += "FOUND TEXT FIELDS:\t" + numTextItemsTotal + "\n";
strOutput += "******************\n";
enumerateAllTextElements();
strOutput += "\n\nREPLACEMENTS MADE:\t" /*+ numTextItemsReplaced*/ + "\n";
strOutput += "******************\n";
replaceAllFonts();
fl.trace("TOTAL RUNS:\t" + numTotalRuns);
fl.trace("ITEMS FOUND:\t" + numTextItemsTotal);
fl.trace("ITEMS REPLACED:\t" + numTextItemsReplaced);
fl.trace("FONTS FOUND:\t" + fontsFound.length + strFoundFonts + "\n");
fl.trace("DETAILS:");
fl.trace("********");
fl.trace("\n" + strOutput + "\n\n");
fl.trace("********************************************************************");
fl.trace(" END OF FONT REPLACEMENT REPORT");
fl.trace("********************************************************************");
// Saving created report to as file
fl.outputPanel.save("file:///d|/Projects/eLearn2/reports/" + dom.name + "-report.txt");
fl.outputPanel.clear();
// NOW WRITE SOME INFO JUST ABOUT FONTS FOR STATISTICS
fl.trace("\n==========================");
fl.trace("FONTS INFO FOR FILE:\t" + dom.name + ".fla");
fl.trace("FILE PATH:\t" + dom.path + "\n");
fl.trace("FONTS FOUND:\t" + fontsFound.length + strFoundFonts + "\n");
// Append font report to file
fl.outputPanel.save("file:///d|/Projects/eLearn2/reports/global-font-report.txt", true);
dom.publish();
dom.saveAndCompact();
dom.close();
|