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

Trying to use ScriptUI to create a text layer and then style it. Can't do either!

Contributor ,
Dec 07, 2019 Dec 07, 2019

Copy link to clipboard

Copied

I have simple ScriptUI button that creates a 4 digit timecode and places it in the bottom left of any size comp. It works, but with a few problems…

 

 

app.project.activeItem.layer(1).text.sourceText.expression = "Numbers = 4; t =timeToFrames(time); c = t.toString().length; while( c < Numbers){ t = \"0\"+t; c++ } t";

app.project.activeItem.layer(1).position.expression = "[20,thisComp.height-20]";

 

 

 

The first problem is I can't figure out how to create a text layer automatically, at the start of this script. So I have to manually create the text layer myself (who has time for that? haha).

 

To create the text layer with scripting i've tried this at the top of the script…

 

 

app.project.item(index).layers.addText(sourceText);

 

 

or…

 

 

app.project.activeItem.layer(1).addText(sourceText);

 

 

but neither work. Neither does calling the exact command ID number which is apparently 7028 or 7058. Neither of the numbers do anything (but they may be out of date, is there a more current list somewhere?).

 

The second problem is I'm unable to style the text in any way, so it always ends up taking on the style of the last used peice of text. I want to use the Monaco font (and if possible: 40px size, white fill, black outline 10px, All Fills Over All Strokes). I've tried… 

 

 

textDocument.font = "Monaco";

 

 

According to Apple's Font Book, this is the correct PostScript name. But this line on it's own does nothing. I assume it might need to be added to the object path, however this doesn't work either…

 

 

app.project.item(index).layers.textDocument.font = "Monaco";

 

 

 

TOPICS
Expressions , Scripting , User interface or workspaces

Views

597

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

Contributor , Dec 08, 2019 Dec 08, 2019

Ok I got to the bottom of it thanks to some help from Dan Ebberts on Creative Cow. For some reason (unknown to him or myself) you need to have textProp.setValue(textDocument); twice. Unsure if this is a bug or a feature.

 

For anyone out there who's interested in the final working script – this will add a 4 digit frame timecode to the bottom left of any size comp. You can execute this as a script or via a Script UI button.

 

var text = app.project.activeItem.layers.addText();

app.project.activeItem
...

Votes

Translate

Translate
Contributor ,
Dec 07, 2019 Dec 07, 2019

Copy link to clipboard

Copied

To answer your first problem, your first line of code should work, given that app.project.item(index) is a Composition. I find it much easier to use app.project.activeItem, but as long as index is valid and so is the Composition, it should work fine. (These basic concepts are all available in the scripting guide and the many tutorials online)

 

As for number 2, changing the style of your text layer, this is also fairly simple and explained in the guide. It is pretty important you use the textProp and textDocument variables and not combine them!

 

var textProp = myTextLayer.property("Source Text");
var textDocument = textProp.value;
myString = "Happy holidays!";
textDocument.resetCharStyle();
textDocument.fontSize = 60;
textDocument.fillColor = [1, 0, 0];
textDocument.strokeColor = [0, 1, 0];
textDocument.strokeWidth = 2;
textDocument.font = "TimesNewRomanPSMT";
textDocument.strokeOverFill = true;
textDocument.applyStroke = true;
textDocument.applyFill = true;
textDocument.text = myString;
textDocument.justification = ParagraphJustification.CENTER_JUSTIFY;
textDocument.tracking = 50;
textProp.setValue(textDocument);

This is the example from the scripting guide and should provide all the font properties you need to change!

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
Contributor ,
Dec 07, 2019 Dec 07, 2019

Copy link to clipboard

Copied

Thanks for the reply! Re: the first problem, would it be…

 

app.project.activeItem.layer(1).addText(sourceText);

 

or…

 

app.project.activeItem.addText(sourceText);

 

I ask because if I'm creating a *new* layer than I assume I don't need to have a current layer selected as an object? So I would assume option 2 here? In any case, neither of these work for me 😞

 

And re: problem 2, I already tried a block of code exactly like that from the scripting guide (and the one you posted above too) and neither work 😞

 

I've confirmed that the button itself works by pasting a completely different script in there and launching it.

 

I've pasted my complete button code here, and even commented out my timecode section if that's any help? I'm not sure where I'm going wrong here.

 

myPanel.grp.group14.timeCodeButton.onClick = function() {

			// app.project.activeItem.addText(sourceText);
			// app.project.activeItem.layer(1).text.sourceText.expression = "Numbers = 4; t =timeToFrames(time); c = t.toString().length; while( c < Numbers){ t = \"0\"+t; c++ } t";
			// app.project.activeItem.layer(1).position.expression = "[20,thisComp.height-20]";
			var textProp = myTextLayer.property("Source Text");
			var textDocument = textProp.value;
			myString = "Happy holidays!";
			textDocument.resetCharStyle();
			textDocument.fontSize = 60;
			textDocument.fillColor = [1, 0, 0];
			textDocument.strokeColor = [0, 1, 0];
			textDocument.strokeWidth = 2;
			textDocument.font = "TimesNewRomanPSMT";
			textDocument.strokeOverFill = true;
			textDocument.applyStroke = true;
			textDocument.applyFill = true;
			textDocument.text = myString;
			textDocument.justification = ParagraphJustification.CENTER_JUSTIFY;
			textDocument.tracking = 50;
			textProp.setValue(textDocument);
         }

 

 

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
Contributor ,
Dec 07, 2019 Dec 07, 2019

Copy link to clipboard

Copied

Even stripping out the style options doesn't help. It still does nothing.

var textProp = myTextLayer.property("Source Text");
var textDocument = textProp.value;
myString = "Happy holidays!";
textDocument.resetCharStyle();
textDocument.text = myString;
textProp.setValue(textDocument);

 Does the text layer have to be selected before I run the script? Does it have to be a certain layer name?

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
Contributor ,
Dec 07, 2019 Dec 07, 2019

Copy link to clipboard

Copied

It's very difficult to tell the scope of the variables you are using or if they exist without seeing all the code. 

 

1) When creating a text layer, you need to refer to a composition and its layers. This will work no matter what, unless you do not have a composition selected, or your sourceText is not a valid string.

var text = app.project.activeItem.layers.addText(sourceText);

 2) In your timecode.onClick function, you are setting the text properties for "myTextLayer". Is this defined anywhere in your code? If it's not then you will need to put the text variable from the previous code in there.

var textProp = text.property("Source 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
Contributor ,
Dec 07, 2019 Dec 07, 2019

Copy link to clipboard

Copied

Ok, I'm making progress! I needed to remove the sourceText variable from the first line (below). I can now create a text layer! The Happy Holidays template worked.

 

And adapting to my version, the following works, but almost all the styling options are ignored (except justify left). When I run it as a standalone (non Script UI) script, I get no errors either. I know the font name could possible be wrong (but im 99% sure it is not) however it's weird that the other options such as font size and color are ignored. These are not my chosen colors or sized btw - i just picked them so I would notice any obvious changes while I tinker with it…

var text = app.project.activeItem.layers.addText();
			
app.project.activeItem.layer(1).text.sourceText.expression = "Numbers = 4; t =timeToFrames(time); c = t.toString().length; while( c < Numbers){ t = \"0\"+t; c++ } t";
app.project.activeItem.layer(1).position.expression = "[20,thisComp.height-20]";

var textProp = text.property("Source Text");
var textDocument = textProp.value;
myString = "Timecode";
textDocument.resetCharStyle();
textDocument.fontSize = 400;
textDocument.fillColor = [0.5, 0, 0];
textDocument.strokeColor = [0, 0.5, 0];
textDocument.strokeWidth = 2;
textDocument.font = "Monaco";
textDocument.strokeOverFill = false;
textDocument.applyStroke = true;
textDocument.applyFill = true;
textDocument.text = myString;
textDocument.justification = ParagraphJustification.LEFT_JUSTIFY;
textDocument.tracking = 50;
textProp.setValue(textDocument);

 

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
Contributor ,
Dec 08, 2019 Dec 08, 2019

Copy link to clipboard

Copied

Ok I got to the bottom of it thanks to some help from Dan Ebberts on Creative Cow. For some reason (unknown to him or myself) you need to have textProp.setValue(textDocument); twice. Unsure if this is a bug or a feature.

 

For anyone out there who's interested in the final working script – this will add a 4 digit frame timecode to the bottom left of any size comp. You can execute this as a script or via a Script UI button.

 

var text = app.project.activeItem.layers.addText();

app.project.activeItem.layer(1).text.sourceText.expression = "Numbers = 4; t =timeToFrames(time); c = t.toString().length; while( c < Numbers){ t = \"0\"+t; c++ } t";
app.project.activeItem.layer(1).position.expression = "[10,thisComp.height-13]";

var textProp = text.property("Source Text");
var textDocument = textProp.value;
myString = "Timecode";
textDocument.resetCharStyle();
textDocument.fontSize = 40;
textDocument.fillColor = [1, 1, 1];
textDocument.strokeColor = [0, 0, 0];
textDocument.strokeWidth = 8;
textDocument.font = "Monaco";
textDocument.strokeOverFill = false;
textDocument.applyStroke = true;
textDocument.applyFill = true;
textDocument.text = myString;
textDocument.justification = ParagraphJustification.LEFT_JUSTIFY;
textDocument.tracking = 45;
textProp.setValue(textDocument);
textProp.setValue(textDocument);

 

 

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
Explorer ,
Feb 22, 2023 Feb 22, 2023

Copy link to clipboard

Copied

LATEST

Hi! could you give a link to your discussion with Dan Ebert

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