• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

194

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

Community Expert , 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
...

Votes

Translate

Translate
Community Expert ,
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);
	}
}

Votes

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.

Votes

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
Community Expert ,
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);
	}
}

Votes

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