ActionScript.org Forums

ActionScript.org Forums (http://www.actionscript.org/forums/index.php3)
-   JSFL - Extending Flash (http://www.actionscript.org/forums/forumdisplay.php3?f=37)
-   -   changing all set fonts in an fla (http://www.actionscript.org/forums/showthread.php3?t=139754)

McBeaver 06-21-2007 06:57 PM

changing all set fonts in an fla
 
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. If I actually click on the textbox and run the script, it will change the font, but if I run the script from the root timeline, it never gets through and Flash shuts it down. My goal would be to just run a script that can grab every file in a directory and find all text elements and change the font, but opening each one individually will be fine too. Is the following code too bulky, searching too much? Thanks.

Code:

var dom = fl.getDocumentDOM();
var font_select = "FranklinGothic-DemiCond";
var font_replace = "Franklin Gothic Demi Cond";


changeFonts = function(timeline) {
    for(var layer in dom.getTimeline().layers){
        for(var frame in dom.getTimeline().layers[layer].frames){
            for(var element in dom.getTimeline().layers[layer].frames[frame].elements) {
                var item = dom.getTimeline().layers[layer].frames[frame].elements[element];
                for(var txt in item.textRuns) {
                    var font = item.textRuns[txt].textAttrs.face;
                    if(font == font_select) item.textRuns[txt].textAttrs.face = font_replace;
                }
            }
        }
    }
}
for(var i=0;i<dom.library.items.length;i++) {
    changeFonts(dom.library.items[i].timeline)
}
changeFonts(dom.getTimeline());

of course if there is a way to do this w/o JSFL, I'm all ears

krayzeebean 06-22-2007 03:22 AM

Have you already tried using Find and Replace? From the Edit menu, not the one from the ActionScript panel.

McBeaver 06-22-2007 03:32 PM

Yeah I tried that. It mostly works, but it seems like the mac font is still present on some newline areas and parts of textboxes I don't even see. I have to select all text manually and change its font.

rost 07-12-2007 12:28 PM

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();


rost 07-12-2007 12:30 PM

And second file for batch font replacement in FLA files
 
And teh second file is just for running first one multiple times for the array of source FLA files (see below):

2.
"C:\Documents and Settings\rost\Local Settings\Application Data\Macromedia\Flash 8\en\Configuration\Commands\[FR] Batch Replace Fonts.jsfl"

Code:

fl.outputPanel.clear();

fl.trace("******************************************************");
fl.trace("  BATCH REPLACE FONTS IN MULTIPLE FILES");
fl.trace("  Author: Rostislav Siryk");
fl.trace("  http://flash-ripper.com/en/");
fl.trace("******************************************************\n");

function getDuration(tEnd, tStart)
{
        var msecsDur = tEnd - tStart;
       
        var msecs = String(msecsDur).substr(String(msecsDur).length - 3);
        var secs = Math.floor(msecsDur / 1000);
        var mins = Math.floor(msecsDur / 60000);
        var hrs = Math.floor(msecsDur / 3600000);

        if (hrs < 10) hrs = "0" + hrs;
        if (mins < 10) mins = "0" + mins
        if (secs < 10) secs = "0" + secs;
        if (msecs < 10) msecs = "00" + msecs;
        else if (msecs < 100) msecs = "0" + msecs;
       
       
        return {durString: hrs + ":" + mins + ":" + secs + "." + msecs, durValue:msecsDur};
}

function getTimeString(aDate)
{
        time = aDate.getTime();
        hours = aDate.getHours();
        minutes = aDate.getMinutes();
        seconds = aDate.getSeconds();
        milliseconds = aDate.getMilliseconds();
        if (minutes < 10) minutes = "0" + minutes
        if (seconds < 10) seconds = "0" + seconds;
        if (milliseconds < 10) milliseconds = "00" + milliseconds;
        else if (milliseconds < 100) milliseconds = "0" + milliseconds;

        return hours + ":" + minutes + ":" + seconds + ":" + milliseconds;
}

function getCurrentTime()
{
        var dateNow = new Date();
        return {timeString: getTimeString(dateNow), timeValue:time};
}

var timeStart = getCurrentTime().timeString;
var timeStartValue = getCurrentTime().timeValue;

var numErrors = 0;

scriptFile="file:///c|/Documents and Settings/rost/Local Settings/Application Data/Macromedia/Flash 8/en/Configuration/Commands/[FR] Replace Fonts.jsfl"

// ADVANCED COURSE FILES LIST
strPathsAdv =
"file:///d|/Projects/eLearn2/Fla/sec01/s02_01_01.fla#" +
"file:///d|/Projects/eLearn2/Fla/sec01/s02_01_02.fla#" +
"file:///d|/Projects/eLearn2/Fla/sec01/s02_01_03.fla#" +
"file:///d|/Projects/eLearn2Fla/site_files/ssecondary_4.fla#";


// BASIC COURSE FILES LIST
strPathsBasic =
"file:///d|/Projects/eLearn2/Fla/sec01/s01_01_01.fla#"+
"file:///d|/Projects/eLearn2/Fla/sec01/s01_01_02.fla#"+
"file:///d|/Projects/eLearn2/Fla/sec01/s01_01_03.fla#"+
"file:///d|/Projects/eLearn2/Fla/site_files/ssecondary_4.fla"

fileStr = strPathsAdv + strPathsBasic;

if(fileStr.length > 0 && scriptFile.length > 0)
{
        var files = fileStr.split("#")
        var numFiles = files.length
       
        for(var f = 0; f < numFiles; ++f)
        {
                if(fl.fileExists(files[f]))
                {
                        var tStart = getCurrentTime().timeString;
                        var tStartValue = getCurrentTime().timeValue;
                        fl.trace("Open File:\t" + files[f]);
                        fl.openDocument(files[f])
                        fl.runScript(scriptFile)
                        fl.trace("\tFILE COMPLETE");
                        fl.trace("\tSTART TIME:\t" + tStart + "\t(" + tStartValue + " msec)");
                        fl.trace("\tEND TIME:\t" + getCurrentTime().timeString + "\t(" + getCurrentTime().timeValue + " msec)");
                        fl.trace("\tDURATION:\t" + getDuration(getCurrentTime().timeValue, tStartValue).durString + "\t(" + getDuration(getCurrentTime().timeValue, tStartValue).durValue + " msec)\n");
                }
                else
                {
                        fl.trace("ERROR OPENING FILE: File doesn't exist:\t"+ files[f] + "\n");
                        numErrors++;
                }
        }
}

fl.trace("\n******************************************************");
fl.trace("FILES TOTAL:\t" + numFiles);
fl.trace("ERRORS TOTAL:\t" + numErrors);

fl.trace("\nSTART TIME:\t" + timeStart + "\t(" + timeStartValue + " msec)");
fl.trace("END TIME:\t" + getCurrentTime().timeString + "\t(" + getCurrentTime().timeValue + " msec)");
fl.trace("DURATION TOTAL:\t" + getDuration(getCurrentTime().timeValue, timeStartValue).durString + "\t(" + getDuration(getCurrentTime().timeValue, timeStartValue).durValue + " msec)");
fl.trace("******************************************************");

// Save report to file
fl.outputPanel.save("file:///d|/Projects/eLearn2/reports/batch-font-replace-report.txt");

Hope this will help.

trymedo 08-27-2008 01:27 PM

Hi,

I've been trying to get to grips with JSFL today, but Im having trouble getting to most of the threads in this section of the site.

I found this one via google, and its pretty close to what I'm trying to achieve. The only difference is that I would like to replace all the fontRenderingMode attributes on the textfields to "advanced"

I have to go through a lot of old FLA's and replace the text aliasing from Animation to Readability so this would save me sooo much time! :)


Is there anyone that could help with this? or is there a thread about it already? If there is would someone be so kind as to link me to it. when I goto http://www.actionscript.org/forums/f...play.php3?f=37 I see the page, but it only displays 4 posts. Although it says theres about 3000.

Thanks to anyone who can help. I'll check back later incase the site is jst playing up :)

Infinite 03-09-2009 08:12 PM

I was messing around with your Script and it doesn't find TextFields that are in a Group. If you take a Text Field and Group it Flash can't find it. :/

I personally don't do this very much but other Flash authors do...


All times are GMT. The time now is 03:20 PM.

Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger