FORUMS: list search recent posts

Change compositions names with text from external file

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Nuria Sanchez
Change compositions names with text from external file
on Mar 2, 2016 at 2:36:25 pm

Hello,
i have a new project which helps me render dozens of clips. The simplified version of it can be found in the attached file. I have only put 3 compositions in it for example, but my project can have up to 100 compositions. Dovnload here my project file



The project reads some texts from a .txt file for the text layers that i have using expressions, and simplify a lot my work.
The .txt file is located on Desktop and look like this:

var Text1 = ["The First Text"];
var Text2 = ["The Second Text"];
var Text3 = ["The Third Text"];
var Clip1 = ["This Is The First Text Video"];
var Clip2 = ["This Is The Second Text Video"];
var Clip3 = ["This Is The Third Text Video"];

Can anybody help me with a script that automatically changes "Clip1" "Clip2" and "Clip3" compositions name withe the corresponding text found on the .txt file please?

Thanks in advance!


Return to posts index

David Conklin
Re: Change compositions names with text from external file
on Mar 2, 2016 at 6:52:43 pm

Hello friend,

Reading text from an external file is somewhat tricky, especially if you want to treat that text as javascript code and not raw text data. I have a solution for you, but it involves slightly re-structuring your text file. If you are desperate for a text file that has javascript inside of it, you can check out JSON parsing. Lloyd Alverez did a good tutorial about parsing JSON if you want to check that out.

Anyway, if you structure your text file with a 'split' character, which differentiates the comp name from the text to change, you can have your script extract 2 pieces of data from that single line. I've created a test text file which does this, following the 'Comp Name/Text Content' format. Keep in mind, that I am using the "/" character here to split, so this won't work if your text or comp name has a slash in it.

Text file looks like this:
Text1/This is the first text.
Text2/This is the second text.
Text3/This is the third text.
Clip1/This is the first text video.
Clip2/This is the second text video.
Clip3/This is the third text video.


And we can use a script to split each of those lines. The script then finds a comp that has the same name as the pre-/ part of the line, and changes it's text layer's text to be the post-/ part.

Disclaimer: this was written for only testing purposes. It does not have fallbacks for if you have 2 comps named the same thing or if it can't find a comp. It also assumes each comp has only 1 layer and that layer is a text layer. With a few tweaks, though, you should be able to make it work in a production environment.

The script:
(function renameItemsFromFile(){

app.beginUndoGroup("Rename items from file.");

// request file.
var myFile = File.openDialog("Select the file."); // get the file.
if (myFile != null){ // check that file was selected.
var fileOK = myFile.open("r"); // open the file in read mode.
if(fileOK){ // make sure we can read the file.

var proj = app.project; // the current project.
if (!proj) return alert("Please have an after effects file open!");

var curLn; // holder for current line.
while(!myFile.eof){ // check that we're not at the end.
curLn = myFile.readln(); // read current line.

if (curLn == "") { curLn == "\r" } // line's empty. skip.

curLn = curLn.split("/"); // break line at slash.

cmp = curLn[0]; // the part before the slash.
txt = curLn[1]; // the part after the slash.

for (var i = 1; i <= proj.numItems; ++i){ // loop through all project items

if(proj.item(i).name == cmp){ // comp name matches.
proj.item(i).layer(1).text.sourceText.setValue(txt); // Change text.
}
}


}

}
}

app.endUndoGroup();

})();


Best of luck!

David Conklin
Motion Designer


Return to posts index

David Conklin
Re: Change compositions names with text from external file
on Mar 2, 2016 at 6:55:08 pm

Also, giving credit where credit is due: the majority of this code is lifted directly from Dan's fantastic case study on reading an external text file on motion script. I'd highly recommend checking it out as he is more thorough than I have been here.

David Conklin
Motion Designer


Return to posts index


Nuria Sanchez
Re: Change compositions names with text from external file
on Mar 2, 2016 at 11:31:37 pm

Thank you for helping me.
I only understand a little javascript, but i think that the script does not do what i need....

I need a script to change the names of the compositions....for example:

The composition "Clip1" becomes "The First Video Title"
The composition "Clip2" becomes "The Second Video Title"
The composition "Clip3" becomes "The Third Video Title"

so let's forget about parsing text fron external file for now, can you help me with a script that simply do this: give to those compositions from the progect new names?

Thank you in advance


Return to posts index

David Conklin
Re: Change compositions names with text from external file
on Mar 3, 2016 at 4:15:20 pm

Hey there, sorry for misunderstanding.

A quick change on line 28 should do what you want (with also reading from the same slash-split external file).

In general, if you want to rename some project items, you can do it like this:

(function renameProjectItems(){

var proj = app.project;

var searchName = "Clip1";
var newName = "This is the first text video";

for (var i = 1; i <= proj.numItems; ++i){
if (proj.item(i).name == searchName){
proj.item(i).name = newName
}
}

})();


You basically loop through all the project items until you find one that has the same name as the 'searchName' var, then change that item's name to the value of newName.

Here's the updated script, which should now change the name of the comp, rather than the text layer inside of the comp:

(function renameItemsFromFile(){

app.beginUndoGroup("Rename items from file.");

// request file.
var myFile = File.openDialog("Select the file."); // get the file.
if (myFile != null){ // check that file was selected.
var fileOK = myFile.open("r"); // open the file in read mode.
if(fileOK){ // make sure we can read the file.

var proj = app.project; // the current project.
if (!proj) return alert("Please have an after effects file open!");

var curLn; // holder for current line.
while(!myFile.eof){ // check that we're not at the end.
curLn = myFile.readln(); // read current line.

if (curLn == "") { curLn == "\r" } // line's empty. skip.

curLn = curLn.split("/"); // break line at slash.

cmp = curLn[0]; // the part before the slash.
txt = curLn[1]; // the part after the slash.

for (var i = 1; i <= proj.numItems; ++i){ // loop through all project items

if(proj.item(i).name == cmp){ // comp name matches.
proj.item(i).name = txt; // Change text.
}
}


}

}
}

app.endUndoGroup();

})();


My text file (still) looks like this:

Text1/This is the first text.
Text2/This is the second text.
Text3/This is the third text.
Clip1/This is the first text video.
Clip2/This is the second text video.
Clip3/This is the third text video.


David Conklin
Motion Designer


Return to posts index

Nuria Sanchez
Re: Change compositions names with text from external file
on Mar 4, 2016 at 6:53:15 pm

Thanks a lot, it works!


Return to posts index

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