Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Layer renaming script problem

  • Layer renaming script problem

    Posted by Adam Levine on May 7, 2016 at 7:26 am

    I’m a beginner for scripting and I created simple script for my own purpose rename layer for active comp and for all comps in the project. That 2 codes are ok. But I’ve no idea how to combined both of them to one UI. However I combined them. But in that script active comp part only working. can anyone help me how to fix that. I think Master Dan can give me an Idea to fix this 🙂

    function stingStartsWith (string, prefix) {
    return string.slice(0, prefix.length) == prefix;
    }

    app.beginUndoGroup("My Undo");

    function allComps() {
    for (var j=1; j<=app.project.numItems; j++){
    var comp = app.project.item(j);
    if (comp instanceof CompItem){
    for (var i=1; i<=comp.layers.length; i++){
    var myLayer = comp.layers[i];
    var myLayerstartswith = stingStartsWith (myLayer.name, UI.mainTextGroup.mainText.text);
    if (myLayerstartswith){
    myLayer.selected = true;
    myLayer.name = UI.secondaryTextGroup.secondaryText.text;
    } else {
    myLayer.selected = false;
    }
    }
    }
    }
    }

    function currentComp() {

    var comp = app.project.activeItem;

    if (comp instanceof CompItem){
    for (var i=1; i<=comp.layers.length; i++){
    var myLayer = comp.layers[i];
    var myLayerstartswith = stingStartsWith (myLayer.name, UI.mainTextGroup.mainText.text);
    if (myLayerstartswith){
    myLayer.selected = true;
    myLayer.name = UI.secondaryTextGroup.secondaryText.text;
    } else {
    myLayer.selected = false;
    }
    }
    } else {
    alert("Please select a composition");
    }
    }

    app.endUndoGroup();

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    function createUserInterface (thisObj,userInterfaceString,scriptName){
    var pal = (thisObj instanceof Panel) ? thisObj : new Window("palette", scriptName,
    undefined,{resizeable: true});
    if (pal == null) return pal;

    var UI=pal.add(userInterfaceString);

    pal.layout.layout(true);
    pal.layout.resize();
    pal.onResizing = pal.onResize = function () {
    this.layout.resize();
    }
    if ((pal != null) && (pal instanceof Window)) {
    pal.show();
    }
    return UI;
    };

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    var resourceString =
    "group{orientation:'column', alignment:['fill','fill'],alignChildren:['left','top']\
    mainTextGroup: Group{orientation:'row',\
    mainTextLabel:StaticText{text:'Find', preferredSize:[80,-1]},\
    mainText: EditText{text:'enter find text here', characters:40}\
    },\
    secondaryTextGroup: Group{orientation:'row',\
    secondaryTextLabel:StaticText{text:'Replace', preferredSize:[80,-1]},\
    secondaryText: EditText{text:'enter replace text here', characters:40}\
    },\
    compGroup: Group{orientation:'row', alignment:['center', 'top']\
    currentComp: RadioButton{text:'Current Comp', value:true},\
    allComps: RadioButton{text:'All Comps'},\
    },\
    applyButton: Button{text:'Apply', alignment:['center', 'bottom']}\
    }";

    var UI = createUserInterface(this,resourceString,"Layer Renamer v1.0");

    if (UI.compGroup.currentComp == true){
    UI.applyButton.onClick = function(){
    currentComp();
    }
    }else if (UI.compGroup.allComps == true){
    UI.applyButton.onClick = function(){
    allComps();
    }

    Adam Levine replied 10 years, 3 months ago 2 Members · 7 Replies
  • 7 Replies
  • Dan Ebberts

    May 7, 2016 at 4:39 pm

    Try changing this section:


    if (UI.compGroup.currentComp == true){
    UI.applyButton.onClick = function(){
    currentComp();
    }
    }else if (UI.compGroup.allComps == true){
    UI.applyButton.onClick = function(){
    allComps();
    }
    }

    to this:


    UI.applyButton.onClick = clickApply;

    function clickApply(){
    if (UI.compGroup.currentComp.value){
    currentComp();
    }else{
    allComps()
    }
    }

    Dan

  • Adam Levine

    May 7, 2016 at 5:14 pm

    Thank you master dan. It works perfect. I need a more help from you. In my script there’s a little problem. If I set something text in the Find and the replace it changing whole layer name to the text I used as Replace text.

    For example: But I want to do like this. If layer name is “My Layer 01” and I input to find text as “My” and input replace text as “White”.
    Then Layer name must change as “White Layer 01” . How to that?

  • Dan Ebberts

    May 7, 2016 at 5:29 pm

    Here’s a simple example that assumes you’re replacing the first part of the name:


    var replacementText = "White";
    var targetText = "My";

    var myLayer = app.project.activeItem.layer(1);
    if (myLayer.name.indexOf(targetText == 0)){
    myLayer.name = replacementText = replacementText + myLayer.name.substr(targetText.length);
    }

    Dan

  • Adam Levine

    May 7, 2016 at 5:55 pm

    Thank you so much master Dan. I need a little more help from you. Can you explain me this codes that you gave me.

    var myLayer = app.project.activeItem.layer(1);
    if (myLayer.name.indexOf(targetText == 0)){
    myLayer.name = replacementText = replacementText + myLayer.name.substr(targetText.length);
    }

  • Adam Levine

    May 7, 2016 at 7:31 pm

    I only want to understand this line

    if (myLayer.name.indexOf(targetText == 0)){

  • Dan Ebberts

    May 7, 2016 at 8:59 pm

    That line just checks to see if the layer name contains the target text, starting at the first character of the name.

    indexOf() returns the zero-based characer index of where it finds (searching from left to right) the first occurrence of the target string, or, -1 if it doesn’t find it at all.

    Dan

  • Adam Levine

    May 8, 2016 at 7:24 am

    Thank you Master Dan

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy