FORUMS: list search recent posts

Layer renaming script problem

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Adam levine
Layer renaming script problem
on May 7, 2016 at 7:26:12 am
Last Edited By Adam levine on May 7, 2016 at 7:29:39 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&lt;=app.project.numItems; j++){
var comp = app.project.item(j);
if (comp instanceof CompItem){
for (var i=1; i&lt;=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&lt;=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();
}


Return to posts index


Dan Ebberts
Re: Layer renaming script problem
on May 7, 2016 at 4:39:47 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



Return to posts index

Adam levine
Re: Layer renaming script problem
on May 7, 2016 at 5:14:50 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?


Return to posts index



Dan Ebberts
Re: Layer renaming script problem
on May 7, 2016 at 5:29:37 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



Return to posts index

Adam levine
Re: Layer renaming script problem
on May 7, 2016 at 5:55:38 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);
}


Return to posts index

Adam levine
Re: Layer renaming script problem
on May 7, 2016 at 7:31:16 pm

I only want to understand this line

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


Return to posts index


Dan Ebberts
Re: Layer renaming script problem
on May 7, 2016 at 8:59:22 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



Return to posts index

Adam levine
Re: Layer renaming script problem
on May 8, 2016 at 7:24:02 am

Thank you Master Dan


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 CreativeCOW.net All Rights Reserved
[TOP]