Skip to main content
Participating Frequently
Oct 26, 2023 11:18 PM UTC
Answered

I DO REALLY NEED HELP WITH THIS SCRIPT! 🙏

 

Hello, I have been trying to figure out how to fix this script because I want it to be dockable, but it only shows a blank window without the layout. I need help. Mathias has been helping me, but I honestly don't know what I am doing wrong. I am very confused. If someone could re-do it for me or help me find the mistake, I would really appreciate it ❤️

// Function to add or update text in the composition
function updateTextInComp(text) {
var comp = app.project.activeItem;

if (comp && comp instanceof CompItem) {
// Check if a text layer already exists
var textLayer = null;
for (var i = 1; i <= comp.layers.length; i++) {
if (comp.layers[i] instanceof TextLayer) {
textLayer = comp.layers[i];
break;
}
}

if (textLayer) {
textLayer.property("Source Text").setValue(text);
} else {
textLayer = comp.layers.addText(text);
textLayer.property("Position").setValue([comp.width / 2, comp.height / 2]);
textLayer.startTime = comp.time;
}
} else {
alert("Please select or create a composition first.");
}
}

 

// Create UI panel
var win = (this instanceof Panel) ? this : new Window("palette", "Text Editor");
win.size = [400, 300];
win.alignChildren = ["fill", "fill"];

 

// Title
var title = win.add("statictext", undefined, "Text Editor");
title.alignment = "center";
title.graphics.font = ScriptUI.newFont("Verdana", "BOLD", 16);

 

// Text Input
var textInput = win.add("edittext", undefined, "", {
multiline: true,
scrolling: true,
enterKeySignalsOnChange: true
});
textInput.size = [undefined, 200];
textInput.graphics.backgroundColor = textInput.graphics.newBrush(
textInput.graphics.BrushType.SOLID_COLOR, [0.2, 0.5, 0.1]
);
textInput.graphics.foregroundColor = textInput.graphics.newPen(
textInput.graphics.PenType.SOLID_COLOR, [0.9, 0.9, 0.9], 1
);

 
// Update Button
var updateButton = win.add("button", undefined, "Update Text");
updateButton.alignment = "center";

updateButton.onClick = function () {
var text = textInput.text;
updateTextInComp(text);
};


if (win instanceof Window) {
    win.layout.layout(true);
    win.layout.resize(); 
    win.show();
}

 


This topic is closed to new replies. Start a new post to keep the conversation going.
Paul TuersleyCorrect answer
Inspiring
Oct 27, 2023 2:53 PM UTC

For scripts launched from the Window menu rather than File > Run Scripts,  After Effects hands the script a panel and you have to feed 'this' into your script and add the UI to it.

I've lifted this dual palette/panel UI framework straight from my own pt_TextEdit script. You might want to save yourself an awful lot of work and just get that instead!

https://aescripts.com/pt_textedit/

 

 

// Function to add or update text in the composition
function textEditor(thisObj) {

	function updateTextInComp(text) {
		var comp = app.project.activeItem;
		
		if (comp && comp instanceof CompItem) {
		// Check if a text layer already exists
			var textLayer = null;
			for (var i = 1; i <= comp.layers.length; i++) {
				if (comp.layers[i] instanceof TextLayer) {
					textLayer = comp.layers[i];
					break;
				}
			}
			
			if (textLayer) {
				textLayer.property("Source Text").setValue(text);
			} else {
				textLayer = comp.layers.addText(text);
				textLayer.property("Position").setValue([comp.width / 2, comp.height / 2]);
				textLayer.startTime = comp.time;
			}
		} else {
			alert("Please select or create a composition first.");
		}
	}
			
	function textEditor_buildUI(thisObj) {

		var win = (thisObj instanceof Panel) ? thisObj : new Window("palette", "Text Editor", undefined, {resizeable:true});
		win.alignChildren = ["fill", "fill"];

		// Title
		var title = win.add("statictext", undefined, "Text Editor");
		title.alignment = "center";
		title.graphics.font = ScriptUI.newFont("Verdana", "BOLD", 16);
		
		// Text Input
		var textInput = win.add("edittext", undefined, "", {
			multiline: true,
			scrolling: true,
			enterKeySignalsOnChange: true
		});
		textInput.size = [undefined, 200];
		textInput.graphics.backgroundColor = textInput.graphics.newBrush(
			textInput.graphics.BrushType.SOLID_COLOR, [0.2, 0.5, 0.1]
		);
		textInput.graphics.foregroundColor = textInput.graphics.newPen(
			textInput.graphics.PenType.SOLID_COLOR, [0.9, 0.9, 0.9], 1
		);
		
		// Update Button
		var updateButton = win.add("button", undefined, "Update Text");
		updateButton.alignment = ["center","bottom"];
		
		updateButton.onClick = function () {
			var text = textInput.text;
			updateTextInComp(text);
		};

		win.onResizing = win.onResize = function () {
    		this.layout.resize();
 		};

		return win;
	}

	textEditorPal = textEditor_buildUI(thisObj);
	if (textEditorPal != null) {

		if (textEditorPal instanceof Window) {
			textEditorPal.center();
			textEditorPal.show();


		} else {
			textEditorPal.layout.layout(true);
		}
	}
}

textEditor(this);

 

4 replies

Paul TuersleyCorrect answer
Inspiring
Oct 27, 2023 2:53 PM UTC

For scripts launched from the Window menu rather than File > Run Scripts,  After Effects hands the script a panel and you have to feed 'this' into your script and add the UI to it.

I've lifted this dual palette/panel UI framework straight from my own pt_TextEdit script. You might want to save yourself an awful lot of work and just get that instead!

https://aescripts.com/pt_textedit/

 

 

// Function to add or update text in the composition
function textEditor(thisObj) {

	function updateTextInComp(text) {
		var comp = app.project.activeItem;
		
		if (comp && comp instanceof CompItem) {
		// Check if a text layer already exists
			var textLayer = null;
			for (var i = 1; i <= comp.layers.length; i++) {
				if (comp.layers[i] instanceof TextLayer) {
					textLayer = comp.layers[i];
					break;
				}
			}
			
			if (textLayer) {
				textLayer.property("Source Text").setValue(text);
			} else {
				textLayer = comp.layers.addText(text);
				textLayer.property("Position").setValue([comp.width / 2, comp.height / 2]);
				textLayer.startTime = comp.time;
			}
		} else {
			alert("Please select or create a composition first.");
		}
	}
			
	function textEditor_buildUI(thisObj) {

		var win = (thisObj instanceof Panel) ? thisObj : new Window("palette", "Text Editor", undefined, {resizeable:true});
		win.alignChildren = ["fill", "fill"];

		// Title
		var title = win.add("statictext", undefined, "Text Editor");
		title.alignment = "center";
		title.graphics.font = ScriptUI.newFont("Verdana", "BOLD", 16);
		
		// Text Input
		var textInput = win.add("edittext", undefined, "", {
			multiline: true,
			scrolling: true,
			enterKeySignalsOnChange: true
		});
		textInput.size = [undefined, 200];
		textInput.graphics.backgroundColor = textInput.graphics.newBrush(
			textInput.graphics.BrushType.SOLID_COLOR, [0.2, 0.5, 0.1]
		);
		textInput.graphics.foregroundColor = textInput.graphics.newPen(
			textInput.graphics.PenType.SOLID_COLOR, [0.9, 0.9, 0.9], 1
		);
		
		// Update Button
		var updateButton = win.add("button", undefined, "Update Text");
		updateButton.alignment = ["center","bottom"];
		
		updateButton.onClick = function () {
			var text = textInput.text;
			updateTextInComp(text);
		};

		win.onResizing = win.onResize = function () {
    		this.layout.resize();
 		};

		return win;
	}

	textEditorPal = textEditor_buildUI(thisObj);
	if (textEditorPal != null) {

		if (textEditorPal instanceof Window) {
			textEditorPal.center();
			textEditorPal.show();


		} else {
			textEditorPal.layout.layout(true);
		}
	}
}

textEditor(this);

 

Translate
Leo BledoAuthor
Participating Frequently
Oct 27, 2023 7:33 PM UTC

Thank you very much Paul, it did work! 

Translate
ToolfarmJP
Community Expert
Community Expert
Oct 27, 2023 12:51 AM UTC

ChatGPT or Google Bard may help you to debug the scripts. Have you tried to use them?

 

Translate