Why can't replace value of sourceText?

Enthusiast ,
Jul 18, 2022 Jul 18, 2022

Copy link to clipboard

Copied

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");
}
}

TOPICS
Scripting

Views

45

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Jul 19, 2022 Jul 19, 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 T
...

Likes

Translate

Translate
Adobe Community Professional ,
Jul 19, 2022 Jul 19, 2022

Copy link to clipboard

Copied

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);
	}
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 19, 2022 Jul 19, 2022

Copy link to clipboard

Copied

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

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 19, 2022 Jul 19, 2022

Copy link to clipboard

Copied

LATEST

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);
	}
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines