Copy link to clipboard
Copied
I have already created the index for my document, and now I'm trying to figure out how to automate the action of generating the index and placing it on the last page of the document inside the one and only text frame on that page. I'm new at scripting and can't figure out what I need to do, even after looking at various API references -- any advice?
Hi Rosanne,
you really have to look more into DOM documentation.
A story has a texts object.
A texts object has a move() method with two arguments.
This should work provided there is only one text frame on the last page of your document.
If there is already contents in the text frame it will be erased:
var doc = app.documents[0];
var myIndex = app.documents[0].indexes[0];
/*
// To work with the returned story of method generate()
// will get us nothing. The returned object is a bit "unstabl...
Copy link to clipboard
Copied
Did you have a look at the index.generate() method here? It can take a number of arguments, but simplistically:
var index = app.documents[0].indexes[0];
index.generate(app.documents[0].pages.lastItem());
Generate creates your own story/frames for you.
Copy link to clipboard
Copied
Hi Rosanne,
look into the first three arguments of method generate().
Also look at the object that this method returns: A Story.
I would handle it this way:
[1] Use one of the first three arguments to generate the index.
Set the other two to undefined, I'd set argument four to false and argument five to true.
[2] Move the text of the returned story to the first insertion point of your text frame on the last page of your document.
[3] Remove all text containers of the returned story in [1].
Just tested this and found that the returned story was a bit unstable.
Means, that story.texts[0] returned an error. story.id returned nothing and so on.
Will be back after testing a bit more…
InDesign 2020 on Windows 10.
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Ok. What is actually working after executing index.generate() is looping all stories of the document.
If storyType == StoryTypes.INDEXING_STORY one could move texts[0] of that story to the first insertion point of the text frame on the last page.
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Thanks, but I'm still not understanding how to loop and move the text. This is what I have so far, but it's not working:
Copy link to clipboard
Copied
Hi Rosanne,
you really have to look more into DOM documentation.
A story has a texts object.
A texts object has a move() method with two arguments.
This should work provided there is only one text frame on the last page of your document.
If there is already contents in the text frame it will be erased:
var doc = app.documents[0];
var myIndex = app.documents[0].indexes[0];
/*
// To work with the returned story of method generate()
// will get us nothing. The returned object is a bit "unstable":
var returnedObject = myIndex.generate()
*/
// So we simply run the method and define some of its 5 arguments:
myIndex.generate
(
// [1] on page, spread or master spread
app.documents[0].pages[0] ,
// [2] placePoint
undefined ,
// [3] destinationLayer
undefined ,
// [4] autoflowing
false ,
// [5] includeOverset
true
);
// Get all stories of the document:
var allStoriesArray = doc.stories.everyItem().getElements();
// Remove all contents from the target:
var targetTextFrame = doc.pages[-1].textFrames[0];
var targetStory = targetTextFrame.parentStory;
targetStory.contents = "";
// Loop the stories
// If indexing story is found move the text to the target
// Remove all text containers of the indexing story
for( var n=0; n<allStoriesArray.length; n++ )
{
if( allStoriesArray[n].storyType == StoryTypes.INDEXING_STORY )
{
allStoriesArray[n].texts[0].move
(
LocationOptions.AT_BEGINNING ,
targetStory.insertionPoints[0]
);
var textContainers = allStoriesArray[n].textContainers;
for( var c=0; c<textContainers.length; c++ )
{
textContainers[c].remove();
};
break;
};
};
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
That was it, thank you so much!
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more