Page 1 of 1

Feedback wanted: Fit Layers To

Posted: September 4th, 2012, 8:46 am
by Klustre
Hi everyone,

As I'm starting out with scripting I'd love to hear any feedback on the script below. It seems to work fluently (CS5), including alerts when the wrong items are selected. It's pretty simple and nothing fancy, but I wonder where I could write it cleaner or better or whatever.

http://upload.klustre.nl/kl_fitLayersTo.jsx.zip

Code: Select all

/*
    
   FIT LAYERS TO—
   PROVIDES A PALETTE WITH BUTTONS TO FIT THE SELECTED LAYERS
   TO EITHER THE CURRENT TIME, WORK AREA OR TIMELINE.
   
   WRITTEN BY REMCO JANSSEN / KLUSTRE
   KLUSTRE.NL / SEPTEMBER 2012

*/

var win = new Window('palette', 'Fit selected layers to:');
this.windowRef = win;

win.grp = win.add("group", [0,0,310,25], 'Group');

var btnWidth = 100;
var btnHeight = 25;

win.grp.fttBtn = win.grp.add("button", [0, 0, btnWidth, btnHeight], 'Timeline');
win.grp.ftwBtn = win.grp.add("button", [105, 0, 105+btnWidth, btnHeight], 'Work Area');
win.grp.ctiBtn = win.grp.add("button", [210, 0, 210+btnWidth, btnHeight], 'Current Time');
 
win.show();

win.grp.fttBtn.onClick = fitToTimeline;
win.grp.ftwBtn.onClick = fitToWorkArea;
win.grp.ctiBtn.onClick = fitToCTI;


function fitToTimeline() {
    
    var klComp = app.project.activeItem;
    
    if(klComp == null) {
        alert("Select at least one layer.");
    } else{

        var klLayers = klComp.selectedLayers;
    
        if((klComp instanceof CompItem) == false || klLayers.length == 0)  { // klLayers.length is 0 when nothing is selected.
            alert("Select at least one layer.");
        } else{
        
            app.beginUndoGroup("Fit to Timeline");
            
                for(var i = 0; i < klLayers.length; i++){ 
                    klLayers[i].inPoint = 0;
                    klLayers[i].outPoint = klComp.duration;
                    }
        
            app.endUndoGroup();
        
        }
    }
}

function fitToWorkArea() {
    
    var klComp = app.project.activeItem;
    
    if(klComp == null) {
        alert("Select at least one layer.");
    } else{

        var klLayers = klComp.selectedLayers;
    
        if((klComp instanceof CompItem) == false || klLayers.length == 0)  { // klLayers.length is 0 when nothing is selected.
            alert("Select at least one layer.");
        } else{   
            
            app.beginUndoGroup("Fit to Work Area");
        
            var waStart = klComp.workAreaStart;
            var waDur = klComp.workAreaDuration;
            
            for(var i = 0; i < klLayers.length; i++){ // klLayers.length is 0 when nothing is selected.
                    klLayers[i].inPoint = waStart;
                    klLayers[i].outPoint = waStart + waDur;
                    }
            
            app.endUndoGroup();
            
        }
    }
}


function fitToCTI() {
    
    var klComp = app.project.activeItem;
    
    if(klComp == null) {
        alert("Select at least one layer.");
    } else{

        var klLayers = klComp.selectedLayers;
    
        if((klComp instanceof CompItem) == false || klLayers.length == 0)  { // klLayers.length is 0 when nothing is selected.
            alert("Select at least one layer.");
        } else{
        
            app.beginUndoGroup("Fit to Current Time");
            
                var klTime = klLayers[0].time;
                
                for(var i = 0; i < klLayers.length; i++){ 
                    klLayers[i].inPoint = 0;
                    klLayers[i].outPoint = klTime;
                    }
            
            app.endUndoGroup();
            
        }
    }
}

Re: Feedback wanted: Fit Layers To

Posted: October 9th, 2012, 1:48 am
by Alan Eddie
Works well, there is a short cut for the current time - alt+] as far as I know.
You could add in 'trim to layer above' and below? Lloyd Alvarez has a similar script on aescripts.com but if
you have gone that far it would be no problem to you.

Re: Feedback wanted: Fit Layers To

Posted: October 9th, 2012, 2:16 am
by Alan Eddie
Also your script may want to interpret AVItems if they are not active during the work area and their length is not long enough to fill the work area or reach the current time. (Perhaps the in jumps to the work area start or something like that).

Re: Feedback wanted: Fit Layers To

Posted: October 10th, 2012, 4:23 am
by Klustre
Thanks for your feedback! Didn't know about that shortcut yet and it works very well. I'll use it from now on :)

I'm not sure if I'll continue working on this, since it was just made for learning purposes. Might work on an advanced Sequence Layers instead. But your AVItem suggestion sounds challenging! I've since learned a lot about formatting the code, looking at Jeff's scripts and this thread: viewtopic.php?f=8&t=2058

So the If statements in this code could be shorter, the klComp and klLayers vars can be global, and preferably it should move the startTime, not the inPoint. And it should indeed take AVItems into account!

Re: Feedback wanted: Fit Layers To

Posted: October 10th, 2012, 4:50 am
by Alan Eddie
Hi there, don't forget to package the global vars in an object or to do some kind of closure. I was making that mistake for a while - safer to avoid them.

Re: Feedback wanted: Fit Layers To

Posted: October 10th, 2012, 5:00 am
by Klustre
Yes, I'd probably use Jeff's method, like I did here: viewtopic.php?f=3&t=2146 But that would be the Object method. What is it you mean exactly by closure?

Re: Feedback wanted: Fit Layers To

Posted: October 10th, 2012, 7:17 am
by Alan Eddie
Not that I am an expert - these guys explain it much better than I could.

http://stackoverflow.com/questions/1111 ... sures-work

http://davidbcalhoun.com/2011/what-is-a ... javascript