Does the SourceText can be linked to a text (.txt) file?

What type of scripts do you need?

Moderator: byronnash

Post Reply
danilomms
Posts: 1
Joined: August 30th, 2007, 11:21 am
Location: Los Angeles

Do you think that maybe be possible to link the SourceText property
of a "text layer" to read its content from a text file (.txt)

I'm asking that because I made a huge animation with hundreds of
text layers (in Portuguese, because I'm a Brazilian guy living in
Los Angeles) and I was wondering to know if is it possible to link
each text layer from after eff ects to a file.txt because...

I need to change the language of the animation to English and after
to Spanish, Italian, German, etc.. would be easier to change the
linked .txt files using NOTEPAD than to open each composition, each
pre-composition, each layer property, each Text Effect property of
each layer to type the characters in other languages... I already
have all translations to other languages in a text files, and I
would like to link each layer to read each a small text file.

Using Expressions...

I was trying to do something like

text.sourceText= c:\desktop\myfile.txt

or
text.sourceText= c:\desktop\myfile.txt.readln();

or
text.sourceText= c:\desktop\myfile.txt.open();

But it is not working... :( I'm doing my first steps in Expression's
inside AE... Scripting seams to be very dificult to link for each
ayer that I have...

I heard that it is impossible to link the SourceText property to a
file using expressions, but it is possible using scripts..

Is there an easy way to associte the Text property to a .TXT file in my
computer? Maybe using a pluggin?

Thank you very much in advance!
Danilo Moura de Mello e Silva
Los Angeles, CA 90034
Danilo Moura
Mylenium
Posts: 139
Joined: July 20th, 2005, 12:07 am

danilomms wrote:Do you think that maybe be possible to link the SourceText property of a "text layer" to read its content from a text file (.txt)
No, that's not possible. All text is tored in a compound stream keyframe along with the formatting and there's no way to link it with external sources.

Mylenium
[Pour Mylène, ange sur terre]
User avatar
Atomic
Posts: 157
Joined: April 30th, 2007, 5:55 am
Location: United States, Ohio

Usage:
1.) Open After EFfects.
2.) Run JSX.
3.) Browse to your plain ascii text document.

It will create a comp for you with a single text layer that will change text as the playback head moves through the timeline.

NOTE: It is not really linked to the text file. It will have all the text from the file embedded into an expression. So if you change your text file, you will have to re-run this script to regenerate the expression, then paste that new expression back into your final comp. This has not been tested against long text files and there is probably a maximum limit for the size of an expression. Also special characters are a consideration.

Code: Select all

// 
// This script reads a user specified text file and 
// creates a text layer with an expression for each line of text. 
//  new comp called "my text comp" 
// 
// By Atom 
// 9-4-2007 

{ 

  // create undo group 

  app.beginUndoGroup("Create Text Layers From File"); 

  // Prompt user to select text file 

  var myFile = fileGetDialog("Select a text file to open.", "TEXT txt"); 
  if (myFile != null){ 

    // open file 
    var fileOK = myFile.open("r","TEXT","????"); 
    if (fileOK){ 

      // create project if necessary 

      var proj = app.project; 
      if(!proj) proj = app.newProject(); 

      // create new comp named 'my text comp' 

      var compW = 864; // comp width 
      var compH = 486; // comp height 
      var compL = 240;  // comp length (seconds) 
      var compRate = 29.97; // comp frame rate 
      var compBG = [48/255,63/255,84/255] // comp background color 
  
      var myItemCollection = app.project.items; 
      var myComp = myItemCollection.addComp('my text comp',compW,compH,1,compL,compRate); 
      myComp.bgColor = compBG; 

   //Create a text layer. 
   thisTextLayer = myComp.layers.addText("This text will be replaced by expression"); 
      
      // read text lines and create text layer for each 
      // until end-of-file is reached 

	var text; 
	var myCount = 1; 
      	var myStep = 1; 
	var myDuration = 90;
      	var the_expression_header = "thisFrame=timeToFrames(t=time+thisComp.displayStartTime,fps=1.0/thisComp.frameDuration,isDuration=false);\n"; 
	the_expression_header = the_expression_header + "var myDuration=" + myDuration +";\n";
	the_expression_header = the_expression_header + "text.sourceText ='...out of text';\n";	//Make this value blank to avoid dreaded undefined.
	
      	var tempExpression = ""; 
	var the_expression = ""; 
      	while (!myFile.eof) 
      	{ 
   		text = myFile.readln(); 
   		if (text == "") text = "\r" ; 
   		tempExpression = "if (thisFrame < (" + myCount + " * myDuration)){text.sourceText = '" + text +"';}"; 
   		the_expression = tempExpression +"\n" + the_expression; 
   		myCount = myCount + myStep; 
      	} 
   // close the file before exiting 
   myFile.close(); 

   //Assign our massive built up expression to this single text layer. 
   thisTextLayer.property("sourceText").expression = (the_expression_header + the_expression); 

    }else{ 
      alert("File open failed!"); 
    } 
  
  }else{ 
    alert("No text file selected."); 
  } 

  app.endUndoGroup(); 
}

One big caveat is that the single quote is used to wrap the line from the text file into the expression. So if you have a single quote in the text file you are reading, the expression will break. My advice there is to simply "escape" any special characters directly in the source text as needed.

Good luck.

PS: I updated the script so that you can now control the length of the display of each line of text (via myDuration variable in the expression header). The expression is also constructed so that you can simply edit the expression in AE after generation to tweak the speed as needed.
Post Reply