Posted on Sunday 24 April 2005
In order to get better search engine rankings, I regularly duplicate the text in my Flash content, place it in a semantically correct XHTML structure, and use it as alternate content on the corporate site. Now Flash can drop the raw text in HTML comments but I hate it as it has no structure, and text in comments isn't going anywhere as far as Google goes. Additionally, I like to spell-check my stuff in Word, as I find it's generally better at catching grammatical no-noes than Flash's (imho) awful built-in spell checker. So how do you get those Flash strings in a nice structure?
JSFL to the rescue! I named all of the relevant text fields _hxx and _px, where the last number is the order in which I want the text to show within that single frame. Then I hardcoded the symbol names of the movieclips I need the text from into the JSFL file. A little RegEx magic and a boatload of nested loops is all that is needed to extract the text. Here's my script:
fl.outputPanel.clear();
var h1s = ["5 1/2 - Home", "Services", "Portfolio", "Contact"];
var itemstolookup = ["mc acceuil contenu", "mc services contenu", "mc portfolio contenu", "mc contact contenu"];
var library = fl.getDocumentDOM().library;
var str = "";
for(var i = 0; i < itemstolookup.length; i++)
{
str += "<h1>" + h1s[i] + "</h1>\n";
library.editItem(itemstolookup[i]);
var tl = fl.getDocumentDOM().getTimeline();
for(var j = 0; j < tl.layers.length; j++)
{
for(var k = 0; k < tl.layers[j].frames.length; k++)
{
var stack = [];
for(var x = 0; x < tl.layers[j].frames[k].elements.length; x++)
{
var c = tl.layers[j].frames[k].elements[x];
if(c.name != undefined && c.name != '')
{
if(c.name.indexOf('__h') != -1)
{
if(c.elementType == 'text')
{
var regex = /__h([0-9])_([0-9]+)[A-Za-z0-9_]*/
var match = c.name.match(regex);
if(match != undefined)
{
stack[Number(match[2])] = "<h" + match[1] + ">" + c.getTextString()
+ "</h" + match[1] + ">";
}
}
}
if(c.name.indexOf('__p') != -1)
{
if(c.elementType == 'text')
{
var regex = /__p_([0-9]+)[A-Za-z0-9_]*/
var match = c.name.match(regex);
if(match != undefined)
{
stack[Number(match[1])] = "<p>" + c.getTextString().replace("\r\r", "</p>\n<p>").replace("&", "&").replace("\r", "<br/>")
+ "</p>";
}
}
}
}
}
if(stack.length > 0)
{
str += stack.join("\n") + "\n";
}
}
}
}
fl.trace(str);
Array.prototype.join = function(del)
{
if(this.length == 0)
{
return "";
}
else
{
var r = "";
for(var i = 0; i < this.length - 1; i++)
{
if(this[i] != undefined)
{
r += this[i] + del;
}
}
return (r + this[this.length - 1]);
}
}
String.prototype.replace = function(s, r) {
var temp = "", i = 0, tmp2 = this;
while((i=tmp2.indexOf(s))!=-1) {
temp += tmp2.substr(0,i)+r;
tmp2 = tmp2.substr(i+s.length);
}
return temp+tmp2;
}
Notice that JSFL doesn't include Array.join and String.replace by default, so I had to prototype the thing. You won't be able to use the JSFL script as is because it's very specific to my own movie layout but you can use it as a template for your projects should the need arise.


