I have a TextLayer with the text "Hello world, It's great".
It's there any script API I can get each word position?
great, This document help me alot~~~
Unfortunately not. I think the best you can do is use sourceRectAtTime() and keep semding different things through a text layer, measuring the size of all the text, the size of a single line of text, and the size of each word then figuring it out from that. Not ideal but certainly possible.
I thought of a way to create two Texts, one to display all, and the other to display only the portion of the all words....and use the sourceRectAtTime API to get the width.....and compare the two position and width , you can get the word position easily
You can do this using text animators and expressions, plus a little time-based trickery.
Add a Character Value text animator to your text with a Range Selector. Set "Start" to -1, "End" to 0, and on the "Offset" property apply the following expression:
Under the "Advanced" property group, set "Units" to "Index", "Based On" to "Words", "Mode" to "Subtract". Set the "Character Value" property to 32 (which is the space character) and also apply this expression to the "Character Value" property:
if(time >= 0) 0 else value
Once you have set all this up, what you will get is a text layer that displays normally for times greater than or equal to 0 seconds, but that will only display individual words at negative times (it blanks out everything but the word at time = -index). Now if you call sourceRectAtTime(-1) you will get the source rectangle of the first word. -2 will give you the second word, and so on.
This can also work for lines, just change "Based On" to "Lines" and then you can access each line's source rect at the time corresponding to its negative index.
It can really run, and it is very precise, thank you so much~~~~~~