Skip to main content
daitranthanhoa
Inspiring
July 19, 2022
Answered

Why can't replace value of sourceText?

  • July 19, 2022
  • 1 reply
  • 380 views

How can replace a new text to sourceText of Layer?

I had try this code, but it not allow:

 

var firstComp = app.project.item(4);
for(var iLayer=1;iLayer<=firstComp.numLayers;iLayer ++)
{
var firstLayer = firstComp.layer(iLayer);

if( firstLayer.sourceText!=undefined && firstLayer.sourceText.numKeys >0 )
{
   //firstLayer.sourceText.value="abc";
    firstLayer.sourceText.setValue( "abc");
}
}

This topic has been closed for replies.
Correct answer Dan Ebberts

If you want to replace the text at a particular key, you'd need to use keyValue() to retrieve the current value, and setValueAtKey() to replace the value at that key. So if you wanted, for example, to replace the text at the first keyframe, it might look like this:

var comp = app.project.activeItem;
var layer;
var sourceText;
var textDoc;
for (var i = 1; i <= comp.numLayers; i++){
	layer = comp.layer(i);
	 if (layer instanceof TextLayer){
		 sourceText = layer.property("Text").property("Source Text");
		 textDoc = sourceText.keyValue(1);
		 textDoc.text = "abc";
		 sourceText.setValueAtKey(1,textDoc);
	}
}

1 reply

Dan Ebberts
Community Expert
Community Expert
July 19, 2022

Try this:

var comp = app.project.activeItem;
var layer;
var sourceText;
var textDoc;
for (var i = 1; i <= comp.numLayers; i++){
	layer = comp.layer(i);
	 if (layer instanceof TextLayer){
		 sourceText = layer.property("Text").property("Source Text");
		 textDoc = sourceText.value;
		 textDoc.text = "abc";
		 sourceText.setValue(textDoc);
	}
}
daitranthanhoa
Inspiring
July 20, 2022

It only allow set value when sourceText.numKeys  =0,

If sourceText.numKeys >0 , can't replace new text.

Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
July 20, 2022

If you want to replace the text at a particular key, you'd need to use keyValue() to retrieve the current value, and setValueAtKey() to replace the value at that key. So if you wanted, for example, to replace the text at the first keyframe, it might look like this:

var comp = app.project.activeItem;
var layer;
var sourceText;
var textDoc;
for (var i = 1; i <= comp.numLayers; i++){
	layer = comp.layer(i);
	 if (layer instanceof TextLayer){
		 sourceText = layer.property("Text").property("Source Text");
		 textDoc = sourceText.keyValue(1);
		 textDoc.text = "abc";
		 sourceText.setValueAtKey(1,textDoc);
	}
}