Skip to main content
andrewh92075852
Known Participant
October 12, 2022
Question

AE Script to Get Individual Word Position

  • October 12, 2022
  • 2 replies
  • 227 views

In AE I’m trying to get the exact position of individual words within a line, sentence or paragraph on a text layer via a script. I then want to create a separate text layer for each word that exactly overlays the original master text. I can get it somewhat working using various combinations of sourceRectAtTime and baselineLocs but it’s often 1-5 pixels out which leaves it looking weird/fuzzy/bold. Any tips or ideas?

This topic has been closed for replies.

2 replies

Dan Ebberts
Community Expert
Community Expert
October 12, 2022

Try this:

	var masterText = "All I can ever be to you";
	var splitMasterText = masterText.split(" ");
	var masterLayer = app.project.activeItem.layers.addText("");
	masterLayer.name = masterText;
	var masterTextSource = masterLayer.sourceText;
	var masterTextDocument = masterTextSource.value;
	masterTextDocument.justification = ParagraphJustification.LEFT_JUSTIFY;
	masterTextDocument.fillColor = [1, 1, 1];
	masterTextSource.setValue(masterTextDocument);
	var cumulativeText = "";
	var newText ;
	var masterRect;
	var newRect;
	var delta;
	var newTextLayer;
	var newTextSource;
	var newTextDocument;
	var oldPos;
	var newPos;
	
	for (var i = 0; i < splitMasterText.length; i++){
		newText = splitMasterText[i];
		cumulativeText += (i > 0 ? " " : "") + newText;
		masterTextDocument.text = cumulativeText;
		masterTextSource.setValue(masterTextDocument);
		masterRect = masterLayer.sourceRectAtTime(0,false);
		newTextLayer = app.project.activeItem.layers.addText(newText);
		newTextSource = newTextLayer.sourceText;
		newTextDocument = newTextSource.value;
		newTextDocument.fillColor = [1, 0, 0];
		newTextSource.setValue(newTextDocument);
		newRect = newTextLayer.sourceRectAtTime(0,false);
		delta = (masterRect.left + masterRect.width) - (newRect.left + newRect.width);
		oldPos = newTextLayer.property("Position").value;
		newPos = oldPos + [delta,0];
		newTextLayer.property("Position").setValue(newPos);
	}
andrewh92075852
Known Participant
October 13, 2022

That's great! Thank you!

Community Expert
October 12, 2022

Using the SDK you can get the text layer as a series of masks from which you can derive the shape's exact boundries, *BUT* the masks are in random order, and some letters are composed of more than one mask... so telling the letters and words apart is no simple manner.

there's no ready made solution for this. (to the best of my knowledge, of course)