Skip to main content
Envelope of Awesome
Known Participant
August 17, 2020
Answered

fl.findObjectInDocByName() Just doesn't seem to work at all. Am I missing something?

  • August 17, 2020
  • 2 replies
  • 644 views

I have a clean Animate file. 
One graphic object in the library "blueCircle".

and one instance of that object on the main timeline.

I copied the code directly from the documentation.

It fails everytime. 
Am I missing something or is fl.findObjectInDocByName(); no longer supports?

PLEASE HELP!!! I will buy you cookies!

 

var nameToSearchFor = "blueCircle";
var doc = fl.getDocumentDOM();
var results = fl.findObjectInDocByName(nameToSearchFor, doc);
if (results.length > 0) {
alert("success, found " + results.length + " objects");
}
else {
alert("failed, no objects named " + nameToSearchFor + " found");
}

 

 

    This topic has been closed for replies.
    Correct answer Vladin M. Mitov

    As a side suggestion:
    If you want to find an instance of a specific symbol by name (not instance name, but symbol name), you can refer to instance's property called libraryItem. It stores a reference to the real symbol in the library, that the particular instance represents. So, to find all the instances of the symbol blueCircle, you can use:

     

     

    function selectInstancesBySymbolName( symbolName ){
    	
    	// Deselect all in the current frame
    	fl.getDocumentDOM().selectNone();
    	
    	var timeline = fl.getDocumentDOM().getTimeline();
    	var i, cf = timeline.currentFrame, myFrame, j;
    	
    
    	for ( i = 0; i < timeline.layerCount; i++ ){
    		myFrame = timeline.layers[ i ].frames[ cf ];
    		for( j = 0; j < myFrame.elements.length; j++ ){
    			if( myFrame.elements[ j ].libraryItem.name === symbolName ){
    				myFrame.elements[ j ].selected = true;
    			}
    		}
    	}
    }
    
    selectInstancesBySymbolName( "blueCircle" );

     




    2 replies

    Vladin M. Mitov
    Vladin M. MitovCorrect answer
    Inspiring
    August 18, 2020

    As a side suggestion:
    If you want to find an instance of a specific symbol by name (not instance name, but symbol name), you can refer to instance's property called libraryItem. It stores a reference to the real symbol in the library, that the particular instance represents. So, to find all the instances of the symbol blueCircle, you can use:

     

     

    function selectInstancesBySymbolName( symbolName ){
    	
    	// Deselect all in the current frame
    	fl.getDocumentDOM().selectNone();
    	
    	var timeline = fl.getDocumentDOM().getTimeline();
    	var i, cf = timeline.currentFrame, myFrame, j;
    	
    
    	for ( i = 0; i < timeline.layerCount; i++ ){
    		myFrame = timeline.layers[ i ].frames[ cf ];
    		for( j = 0; j < myFrame.elements.length; j++ ){
    			if( myFrame.elements[ j ].libraryItem.name === symbolName ){
    				myFrame.elements[ j ].selected = true;
    			}
    		}
    	}
    }
    
    selectInstancesBySymbolName( "blueCircle" );

     




    - Vlad: UX and graphic design, Flash user since 1998Member of Flanimate Power Tools team - extensions for character animation
    Envelope of Awesome
    Known Participant
    August 18, 2020

    I have no idea how to thankyou!!!
    This is a breath of fresh air!!!
    Thankyou thankyou thankyou!!!

    Vladin M. Mitov
    Inspiring
    August 17, 2020

    I think that this method looks for movie clip instances only. Graphic instances don't have names, so you can't search them by name. In your example, if you change the instance type to "Movie Clip" and assign it an instance name "blueCircle", the method will find the instance.


    - Vlad: UX and graphic design, Flash user since 1998Member of Flanimate Power Tools team - extensions for character animation
    Envelope of Awesome
    Known Participant
    August 18, 2020

    Thankyou SO Much for clarifying this!
    I couldn't find any answers in the documentation. 

    Is there a way to find a specific Graphic Symbol on the stage by name?

    Basically I'm writing a script that changes the FirstFrame of the Head symbol, and then adjusts the "nose" position accordingly. 
    But all the symbols have to remain Graphic symbols.
    Somthing like:

    function HEADANGLE(mousePosX, mousePosY) {
    
    newFirstFrame = mousePosX - mousePosY;
    fl.getDocumentDOM().HEADSYMBOL.setElementProperty('firstFrame', newFirstFrame);
    
    //search and select "nose" Graphic symbol:
    var nameToSearchFor = "nose";
    var doc = fl.getDocumentDOM();
    var results = fl.findObjectInDocByName(nameToSearchFor, doc);
    if (results.length > 0) {
    alert("success, found " + results.length + " objects");
    }
    else {
    alert("failed, no objects named " + nameToSearchFor + " found");
    }
    
    //Move nose accordingly:
    nose.moveX ++;
    nose.moveY ++;
    
    }




    Envelope of Awesome
    Known Participant
    August 18, 2020

    Ok I think I have a work around:

    My layer names should be correct so....
    I can cycle through all the layers and lock all the others and selectAll. 
    Then I can manipulate the selected graphic symbol from there. 

    //////////////////////////Select the right Layer Function:
    	function SELECTLAYER(layerName) {
    		
    		var timeline = fl.getDocumentDOM().getTimeline();
    		
    		//cyle through all the layers and then cycles through all the layer names. If they match, unlock the layer and lock everything else. 
    		for (var i = 0; i < timeline.layerCount; ++i) {
    			fl.trace("layerName========= "+ layerName[i]);
    			//Lock the layer...
    			fl.getDocumentDOM().getTimeline().layers[i].locked = true;
    
    			//cycle through the layer names. 
    			for (j = 0; j < timeline.layerCount; j++) {
    				//fl.trace("In FLA layerNames========= "+ timeline.layers[j].name);
    				//fl.trace("in TXT layerNames========= "+ layerNames[i]);
    				//if there's a match Unlock the layer.
    				if (timeline.layers[i].name == layerName) {
    					fl.getDocumentDOM().getTimeline().layers[i].locked = false;
    					fl.trace("MATCH!!!  " + timeline.layers[i].name + " <is> " + layerName);
    				}
    			}
    		}
    		//select all unlocked items.
    		fl.getDocumentDOM().selectAll();
    		
    		
    		
    	}