Instance names from text file??

127 views
Skip to first unread message

Maurizio

unread,
Jun 24, 2008, 1:10:43 PM6/24/08
to flash.jsfl
Dear all,
i got a problem.
I would like to rename the instance names of movie clips by reading an
external text file.
The instance names are ordered as the movie symbol..so it is a pretty
easy thing...Read the text file>>>first line>>>give the first line as
the instance name of the first movie clip.

I think i probably need a command (jsfl) that automatically parse the
text file and then rename the instance names of the movie clips in the
movie explorer directly.
Can you help me with this problem???
I am a bit desperate..

Quentin

unread,
Jun 24, 2008, 4:39:44 PM6/24/08
to flash.jsfl
Hi there!
You'll need FLfile (http://www.adobe.com/devnet/flash/articles/
jsapi_print.html) to achieve this...

Example (not tested):

var instanceNamesPath="file:///C:/myFolder/instance-names.txt";
var instanceNames=FLfile.read(instanceNamesPath).split('\r\n');
for (var i=0; i<instanceNames.length; i++) {
var curInstanceName=instanceNames[i];
alert(curInstanceName);
}

I hope this helps!
Quentin.

Keith Hair

unread,
Jun 24, 2008, 4:47:25 PM6/24/08
to js...@googlegroups.com
You might be able to do what you need by modifying the functions and script below.
You after using FLfile.read() you can use Regular Expressions to parse the values
you want from each line.

Not sure what you are asking but I hope this helps,

-- Keith H --

//==================

fl.outputPanel.clear ();
var filename="list.txt"; //The text file to read list from.
var list=FLfile.read(getDocumentPath()+"/"+filename);
var lines=list.split("\r\n");
var n = 0;
var results="";
while(n < lines.length)
{
    var element=findObjects(lines[n])[0];
    if(element){
        results+="Found: "+element.name+"\n";
        element.name="NEW_NAME";
    }
    n++;
}
alert(results);

/*----------------------------------------------------------------
getDocumentPath

Return the file URI path of the document running the JSFL script.
------------------------------------------------------------------*/
function getDocumentPath()
{
    var path=String(fl.getDocumentDOM().path).replace(/\\/ig,"/");
    path=path.replace(/:/ig,"|");
    path="file:///"+path;
    path=path.replace(/\/+$/ig,"");
    path=path.substring(0,path.lastIndexOf("/"));
    return path;
}

/*-------------------------------------------------------------------
findObjects

Returns an Array of elements in FLA document that match instanceName.
If nothing matches instanceName, an Array with a length of 0 is returned.
--------------------------------------------------------------------*/
function findObjects (instanceName, inInstance)
{
    var instances = [];   
    var findObjectInTimeline = function (instance, name)
    {
        var timeline;   
        if (instance.toString () == "[object SymbolInstance]" || instance.toString () == "[object Frame]")
        {
            timeline = instance.timeline;
        }
        if (instance.toString () == "[object Document]")
        {
            timeline = instance;
        }
        if (instance.toString () == "[object Timeline]")
        {
            timeline = instance;
        }
        if (instance == fl.getDocumentDOM ())
        {
            timeline = instance.getTimeline ();
        }
        if (timeline == undefined)
        {
            return null;
        }
        var checked = new Object ();
        var currentLayers = timeline.layers;
        var layObj;
        var frm;
        var found;
        var i = 0;
       
        while (i < currentLayers.length)
        {
            var layObj = currentLayers [i];
            var frms = layObj.frames;
            var k = 0;
            while (k < frms.length)
            {
                var selArry = frms [k].elements;
                var n = 0;
                while (n < selArry.length)
                {
                    var obj = selArry [n];               
                    if (obj.name == name)
                    {
                        found = obj;
                        instances.push (obj);
                    }
                    if (obj.libraryItem != undefined)
                    {
                        if (obj.libraryItem.name != undefined)
                        {
                            if (checked [obj.libraryItem.name])
                            {
                                n ++;
                                continue;
                            }else
                            {
                                checked [obj.libraryItem.name] = true;
                            }
                        }
                        obj = findObjectInTimeline (obj.libraryItem, name);
                        if (obj != null)
                        {
                            found = obj
                        }
                        obj = selArry [n];
                        if (obj.libraryItem.timeline != undefined)
                        {
                            obj = findObjectInTimeline (obj.libraryItem.timeline, name);
                            if (obj != null)
                            {
                                found = obj
                            }
                        }
                    }
                    n ++;
                }
                k ++;
            }
            i ++;
        }
        return found;
    }
    if (inInstance == null)
    {
        inInstance = fl.getDocumentDOM ();
    }
    findObjectInTimeline (inInstance, instanceName);
    return instances;
}


-- Keith Hair --
www.keith-hair.com


Maurizio

unread,
Jun 25, 2008, 6:51:12 AM6/25/08
to flash.jsfl
Thank you very much guys... i actually found another solution...i
think this could be useful to other people in my situation..
It uses flafile and reads the text file for the names...(the length of
the text file has to be the same of the movie clips


var instanceNamesPath="file:///C:/text.txt";
var instanceNames=FLfile.read(instanceNamesPath).split('\r\n');
var i =0;
for (var i=0; i<instanceNames.length; i++) {
var curInstanceName=instanceNames[i];
var tl = fl.getDocumentDOM().getTimeline()
var elts =
tl.layers[tl.currentLayer].frames[tl.currentFrame].elements;
var elt = elts[i];
elt.name = curInstanceName;
}


hope is ok!

Ciao and thanks again!

lolo

unread,
Jul 8, 2008, 5:23:53 PM7/8/08
to flash.jsfl
somehow related question...

i have bunch of movie clips on the stage. (5 movie clips). I would
like to give them an instance name to each of them to whatever they
have as movie clips.

ex: if a movie clip on stage is called graphic1 then i would like to
give it an instance name of graphic1

how can i achieve this on my jsfl?

if it helps, each movie clip is already distributed on a separate
layer.

any help would be greatly appreciate it.

thanks :)

Keith Hair

unread,
Jul 8, 2008, 5:44:11 PM7/8/08
to js...@googlegroups.com
Each symbol on the Stage has a "name" property.
You get access it's library name through the it's "libraryItem" property:

If you are looping over elements in a frame on the Timeline
you can assign the "name" property of the element's "libraryItem"
property to it.

var layerIndex=0;
var frameIndex=0;
var elementIndex=0;
var obj=fl.getDocumentDOM().getTimeline().layers[layerIndex].frames[frameIndex].elements[elementIndex];
if (obj.libraryItem.name != undefined){
obj.libraryItem.name=obj.name;
}

--

lolo

unread,
Jul 8, 2008, 5:54:23 PM7/8/08
to flash.jsfl
its telling me that obj has no properties...

here is all my jsfl code

=========

var version = '1.00';
var allowedFiletypes = ['png', 'gif', 'jpg'];
var separator = '/';
var defaultImageQuality = 90;


fl.outputPanel.clear();
fl.trace('Cross your fingers! for the awesome UPS Banner (version ' +
version + ')');

// create new project
fl.createDocument();

// shortcuts
var doc = fl.getDocumentDOM();
var library = doc.library;

// set to player 9
doc.setPlayerVersion(9);

//set stage
doc.width = 710;
doc.height = 200;

//set Frame Rate
doc.frameRate = 24;

// select images to import
var importFolder = fl.browseForFolderURL('Select the folder with
images to convert');
var importFolderContents = FLfile.listFolder(importFolder);
var i;

alert("Choose the folder where you want to save the .fla files");
var bannerFlaFolder = fl.browseForFolderURL("Select fla output
folder.");

// import files into library
for (i = 0; i < importFolderContents.length; i++)
{
var file = importFolderContents[i];
var fileURI = importFolder + separator + file;

if (!isAllowedFiletype(file))
{
continue;
}

fl.trace('Importing ' + filenameFromURI(fileURI) + ' ...');
doc.importFile(fileURI, true);
}

// choose export folder
//var exportFolder = fl.browseForFolderURL('Select the folder to which
you want to export the images to');

// choose image quality
var imageQuality = prompt('Please choose image quality', '90');
if (imageQuality == null)
{
imageQuality = defaultImageQuality;
}
else
{
imageQuality = parseInt(imageQuality);
imageQuality = Math.max(0, imageQuality);
imageQuality = Math.min(100, imageQuality);
}

doc.getTimeline().addNewLayer();
doc.getTimeline().layers[0].name = "as";
doc.getTimeline().layers[0].frames[0].actionScript = 'stop();';
//fl.getDocumentDOM ().getTimeline().insertBlankKeyframe(9);
//fl.getDocumentDOM ().getTimeline().layers[0].frames[9].actionScript
= 'stop();';

var items = library.items.concat();
var mcsFolder = 'converted_movieclips';
i = items.length;


doc.library.selectItem('ups_banner.png');
doc.library.selectItem('ups_banner.png');
doc.library.addItemToDocument({x:286.9, y:69});
doc.moveSelectionBy({x:3.4, y:0});
doc.moveSelectionBy({x:0, y:30.9});
doc.mouseClick({x:188.9, y:89}, false, true);
doc.breakApart();
doc.mouseClick({x:188.9, y:89}, false, true);
doc.distributeToLayers();

doc.setSelectionRect({left:-111, top:-39, right:780.0, bottom:294},
true, true);
doc.moveSelectionBy({x:64.7, y:0});
doc.moveSelectionBy({x:0, y:0.1});


// Give movie clips instance names

var layerIndex=0;
var frameIndex=0;
var elementIndex=0;
var
obj=doc.getTimeline().layers[layerIndex].frames[frameIndex].elements[elementIndex];
/*
* Helper methods
*/

function isAllowedFiletype(file)
{
var suffix = suffixForFile(file).toLowerCase();
var i = allowedFiletypes.length;
while (i--)
{
if (allowedFiletypes[i] == suffix)
{
return true;
}
}
return false;
}

function filenameFromURI(uri)
{
var parts = uri.split(separator);
return parts.pop();
}

function suffixForFile(file)
{
var parts = file.split('.');
return parts[parts.length - 1];
}

//doc.save();
//doc.testMovie(); // or publish()
fl.saveDocument(fl.getDocumentDOM());
fl.closeDocument(fl.getDocumentDOM());

Keith Hair

unread,
Jul 8, 2008, 6:12:24 PM7/8/08
to js...@googlegroups.com
The variables here must be worked into your own script.
I only set them to 0 to simplify my example.

var layerIndex=0;
var frameIndex=0;
var elementIndex=0;

This was for looping though each Layer...looping through each Frame,
and looping through each Element.


On Tue, Jul 8, 2008 at 5:54 PM, lolo <lolo...@gmail.com> wrote:
>
> its telling me that obj has no properties...
>
> here is all my jsfl code
>
> =========

lolo

unread,
Jul 8, 2008, 7:08:49 PM7/8/08
to flash.jsfl
so how do i go by achieving that? not sure wher do i need to modify
the code to make this work.

please help out =/

thanks!
Reply all
Reply to author
Forward
0 new messages