Copy link to clipboard
Copied
Hello
I have a series of text boxes in my document — photo captions with a formatted date.
I would like to add text to the box according to a date calculation (specifying the number of weeks since a fixed point in the past up to the date that exists in the text box.)
I have no problem with the "formula" to compute the number of weeks, so that's not my question.
My question is this: how can I script these text boxes to insert that computed text (as a number)?
I am completely new to InDesign scripting, but fluent in the appropriate languages that seem to be used.
Thanks for pointers to get me started.
Julian
Easiest way to add text to a frame is targeting its last "insertion point", which is equivalent to clicking the text cursor at the very end of its text.
With a text frame selected, this line will add text at the end:
app.selection[0].insertionPoints[-1].contents = "hello, world!";
Copy link to clipboard
Copied
Easiest way to add text to a frame is targeting its last "insertion point", which is equivalent to clicking the text cursor at the very end of its text.
With a text frame selected, this line will add text at the end:
app.selection[0].insertionPoints[-1].contents = "hello, world!";
Copy link to clipboard
Copied
In addition to what [Jongware] mentioned if you want to automatically insert the text into your textframes without selecting them, then you would need a way to identify such frames. One way could be to assign a name to these frames and then add the content to it. Something like the following
var tf = app.documents[0].textFrames.everyItem().getElements()
for(var i = 0 ; i < tf.length; i++)
{
if(tf.name == "myTF") //Change the name to whatever you use
{
tf.insertionPoints[-1].contents = "hello, world!"; //Change it to the result of your formula
}
}
-Manan
Copy link to clipboard
Copied
Manan, I was also thinking of how Julian would identify which text frames to add this to -- but decided not to mention it. There are just too many ways and there is too little information provided (*). So I guess that's under control.
(*) For example,
Copy link to clipboard
Copied
All,
Thank you for the solution and pointers. This will work, although I do have the question as to an example as to how to select, well — selectively. For example, these particular text boxes are unique in that they are all styled using a specific user defined Paragraph Style called "Photo Caption Date". Is there a coding idiom that allows me to identify this in the object model you can point me to?
Thank you for your assistance.
Julian
Copy link to clipboard
Copied
Searching for text in that style is the easiest way:
app.findTextPreferences = null;
app.findTextPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item("Photo Caption Date");
list = app.activeDocument.findText();
Then, "list" contains all of the paragraphs in that style. If you want to add your weeks number to the end of these paragraphs, then you're basically done here and you can add them right away. However, if there may be some other text after this paragraphs, and you want to add it -- as you originally stated -- at the very end of the text frame, then you need the "parent container" of the text you just found:
list[0].parentTextFrames[0].insertionPoints[-1].contents = 'hello world';
(where instead of "list[0]" you'd want to loop over all of the found items).
Copy link to clipboard
Copied
Hi Julian,
from your question I'm not clear if you exactly want to add or change contents of text. And where exactly you want to do this. Also note that text frames could "contain" overset text. Text that cannot show up in the frame because e.g. the frame isn't big enough to hold all the text.
Note: Texts are organized in stories. Always.
One single story can have one or more items in its textContainers array.
Text containers could be textFrames or textPaths.
Text solely in overset has no parent text frame . Means the length of the parentTextFrames array is 0 .
Text that is only partly in overset will show that the length of parentTextFrames is greater than zero. Depending on how many text containers a particular text is running through one or more text containers are stored in this array.
Your particular case:
Since you seem to know that the parent story of the text you are searching for does only have one single text container the first instance of the parentTextFrames array should be the one for your found text. If, of course, parts or all of the found text is visible in the text frame.
FWIW:
For text that is fully in overset , the text container where the overset starts, can be detected with a construction like that:
list[0].parentStory.textContainers[ list[0].parentStory.textContainers.length -1 ];
Or with something like that:
list[0].parentStory.textContainers[0].endTextFrame;
The last insertion point of a text container that contains overset text will be somewhere in the middle of the story that this text frame belongs to.
So with overset text you'd better address the last insertion point of the parent story to add contents to ( I assume you want this, but I'm not sure 😞
list[0].parentStory.insertionPoints[-1].contents = "Hello World";
Or, because list[0] stores a texts object , you add contents to the last insertion points of your found text.
Which could be a different insertion point than the last one in the story.
list[0].insertionPoints[-1].contents = "Hello World";
Why would I recommend this?
Found text could be partially visible in the frame and be partially in overset.
parentTextFrames[0] will store the frame then.
But the last insertion point of that frame is not necessarily the last insertion point of your found text.
Regards,
Uwe
Copy link to clipboard
Copied
So, for completeness: the text in these text boxes contains a date and I am attempting to add the time interval from a fixed point in the past to the date specified. For example, the text box might contain "May 27, 2019" and I want it to also contain the number of weeks from December 31, 2014 so finally the text box would say, "May 27, 2019. Week 230".
function diff_weeks(dt2, dt1)
{
var diff =(dt2.getTime() - dt1.getTime()) / 1000;
diff /= (60 * 60 * 24 * 7);
return Math.abs(Math.round(diff));
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now