Skip to main content
Participating Frequently
June 16, 2022
Answered

Edit layer with code

  • June 16, 2022
  • 2 replies
  • 646 views

Hi all. I'm trying to create snippet/script that is able to edit the selected layer on the timeline.
More specifically I want to add an instance name to a certain movie clip on the selected layer.

I.e. if the user select a layer and execute the script then it should add the same instance name to each key frame on that layer. Hope it makes sense.

I'm not sure if it is event possible to create something like this.
I'm currently staring to learn ActionScript 3.0, so please excuse me for the dumb question.
Any help or resources/guides will be much appreciated.
Thank you.

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

    Save this script as a command and execute it via "Commands" menu.

    (function(){
    			
    	var myInstanceName = prompt( "Instance name:" );
    	if( ! myInstanceName ) return;
    	
    
    	var tml = fl.getDocumentDOM().getTimeline();
    	var myLayer = tml.layers[ tml.currentLayer ];
    	var i, myFrame, j, myElement, myReport = {frameCount:0, elementCount:0};
    
    	// Add 'button' to this array if you want to name the button instances as well
    	var myTypes = [ "movie clip" ];
    	
    	
    	for( i = 0; i < myLayer.frames.length; i++ ){
    		myFrame = myLayer.frames[ i ];
    		
    		// Skip non-keyframes
    		if( myFrame.startFrame != i ) continue;
    		
    		myReport.frameCount ++;
    		
    		for( j = 0; j < myFrame.elements.length; j++ ){
    			
    			myElement = myFrame.elements[ j ];
    			
    			if( myTypes.indexOf( myElement.symbolType ) > -1 ){
    				myElement.name = myInstanceName;
    				myReport.elementCount ++;
    			}
    		}
    	}
    	
    	fl.outputPanel.clear();
    	fl.trace( "Layer: " + myLayer.name );
    	fl.trace( "Frames: " + myReport.frameCount );
    	fl.trace( "Instances: " + myReport.elementCount );
    })();

     

     

    2 replies

    Vladin M. Mitov
    Vladin M. MitovCorrect answer
    Inspiring
    June 17, 2022

    Save this script as a command and execute it via "Commands" menu.

    (function(){
    			
    	var myInstanceName = prompt( "Instance name:" );
    	if( ! myInstanceName ) return;
    	
    
    	var tml = fl.getDocumentDOM().getTimeline();
    	var myLayer = tml.layers[ tml.currentLayer ];
    	var i, myFrame, j, myElement, myReport = {frameCount:0, elementCount:0};
    
    	// Add 'button' to this array if you want to name the button instances as well
    	var myTypes = [ "movie clip" ];
    	
    	
    	for( i = 0; i < myLayer.frames.length; i++ ){
    		myFrame = myLayer.frames[ i ];
    		
    		// Skip non-keyframes
    		if( myFrame.startFrame != i ) continue;
    		
    		myReport.frameCount ++;
    		
    		for( j = 0; j < myFrame.elements.length; j++ ){
    			
    			myElement = myFrame.elements[ j ];
    			
    			if( myTypes.indexOf( myElement.symbolType ) > -1 ){
    				myElement.name = myInstanceName;
    				myReport.elementCount ++;
    			}
    		}
    	}
    	
    	fl.outputPanel.clear();
    	fl.trace( "Layer: " + myLayer.name );
    	fl.trace( "Frames: " + myReport.frameCount );
    	fl.trace( "Instances: " + myReport.elementCount );
    })();

     

     

    - Vlad: UX and graphic design, Flash user since 1998Member of Flanimate Power Tools team - extensions for character animation
    Participating Frequently
    June 20, 2022

    Thank you very much 🙂

    kglad
    Community Expert
    Community Expert
    June 16, 2022

    layers don't exist in swf files, so are you asking about jsfl code to edit your fla?

    Participating Frequently
    June 17, 2022

    Hi @kglad,

    Yes, maybe. I'm sorry if my words sound stupid. As I've mentioned I'm still a beginner when it comes to scripting in Animate and I'm trying to understand how everything works.
    What I want is to create a script that can edit the fla file by adding an instance name to a certain movie clips.
    Could you please refer me to some examples for the Animate's API and/or any examples that manipulates the timeline.
    I couldn't find any sufficient information.

    kglad
    Community Expert
    Community Expert
    June 17, 2022