Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions ExtendScript: Replacing a layer via a function using a != “else” statement.

  • ExtendScript: Replacing a layer via a function using a != “else” statement.

    Posted by Andy Troz on November 15, 2013 at 6:50 am

    Hello,

    I have a simple GUI with a “edittext” box and an apply button that I use to trigger functions.

    I have an instance where I run a for loop to look for the name of a specific composition, so in this case, if I type “player one headshot” into the edittext box, the layer in my comp will be replaced by the “player one headshot” comp in my project window.

    What I need is an “else” statement that says “if the user text in the edit text box doesn’t = any compositions with that name, then replace the layer with a comp named “default composition””

    How would I do this?

    Here’s my working code before the else statement. Feel free to tear this apart as well if there’s a better way to do it:

    function checkPlayerHeadshot(){
    for (var i = 1; i <= app.project.numItems; i++){
    if (app.project.item(i).name == "player one headshot"){
    playerNameText = app.project.item(i);
    playerHeadshot.replaceSource((playerNameText), true);
    break;
    }
    }
    }

    Best,
    Andy.

    Andy Troz replied 12 years, 9 months ago 2 Members · 3 Replies
  • 3 Replies
  • Andy Troz

    November 15, 2013 at 9:25 pm

    Looks like I was able to get it sorted!


    function checkCompName(){
    for (var i = 1; i <= app.project.numItems; i++){
    if (app.project.item(i).name == colorText.text){
    theImage.replaceSource(app.project.item(i), true);
    break;
    }else{
    for (var j = 1; j <= app.project.numItems; j++){
    if (app.project.item(j).name == "Blank"){
    theImage.replaceSource(app.project.item(j), true);
    break;
    }
    }
    }
    }

  • Dan Ebberts

    November 15, 2013 at 10:44 pm

    That looks pretty close, but not quite right. I’d do it more like this (not tested, but should be close):


    function checkCompName(){
    var theReplacement = null;
    for (var i = 1; i <= app.project.numItems; i++){
    if (app.project.item(i).name == colorText.text){
    theReplacement = app.project.item(i);
    break;
    }
    }

    if (theReplacement == null){
    for (var i = 1; i <= app.project.numItems; i++){
    if (app.project.item(i).name == "Blank"){
    theReplacement = app.project.item(i);
    break;
    }
    }
    }

    if (theReplacement != null){
    theImage.replaceSource(theReplacement, true);
    }else{
    alert("Yikes - can't find either replacement.");
    }
    }

    Dan

  • Andy Troz

    November 17, 2013 at 2:15 am

    Thank you Dan! That makes a lot more sense! 😀

    Best,
    Andy.

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