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

TypeScript throws error when trying to use setValue on text layer

Explorer ,
May 13, 2023 May 13, 2023
 

 

var newTextLayer: TextLayer = app.project.activeItem.layers.addText("Hello, world.");
newTextLayer.property("Position").setValue([0,0]);

 

 
TypeScript throws the following error:
Property 'setValue' does not exist on type '_PropertyClasses'.
Property 'setValue' does not exist on type 'PropertyGroup'.
 
Is there a better way to set properties on text layers?
TOPICS
Scripting
1.5K
Translate
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 ,
May 13, 2023 May 13, 2023

I think you just need to change the first line:

var newTextLayer = app.project.activeItem.layers.addText("Hello, world.");
newTextLayer.property("Position").setValue([0,0]);
Translate
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
Explorer ,
May 13, 2023 May 13, 2023

That's how I had it initially when I first got the error. I was hoping specifically setting the variable type to TextLayer might address the issue, but it didn't.

Translate
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 ,
May 13, 2023 May 13, 2023

It works fine for me. I've never seen that error message--I'm not sure how you would get that.

Translate
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
Explorer ,
May 14, 2023 May 14, 2023

Figured it out if anyone else runs into this issue:

 

property("Position") returns a _PropertyClasses

_PropertyClasses is an alias for: Property | PropertyGroup | MaskPropertyGroup

 

But setValue() is only valid for Property. It does not exist for PropertyGroup or MaskPropertyGroup

 

For valid TypeScript, you have to first confirm that property("Position") is specifically a Property before attempting to call setValue(). There are many ways to accomplish this (https://www.typescriptlang.org/docs/handbook/2/narrowing.html), but here was my solution:

 

 

var textPos = newTextLayer.property("Position") as Property;
textPos.setValue([0,0]);

 

 

Translate
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 ,
May 15, 2023 May 15, 2023
LATEST

Sorry, I totally missed the TypeScript reference. Glad you got it sorted!

Translate
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