Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Javascript to generate index

New Here ,
Dec 19, 2020 Dec 19, 2020

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?

TOPICS
Scripting
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 22, 2020 Dec 22, 2020

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
...
Translate
Community Expert ,
Dec 20, 2020 Dec 20, 2020

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. 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 21, 2020 Dec 21, 2020

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 )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 21, 2020 Dec 21, 2020

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 )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 21, 2020 Dec 21, 2020

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:

 

//generate index
var doc = app.documents[0];
var index = doc.indexes[0];
// index.generate(app.documents[0].masterSpreads.lastItem(),undefined,undefined,true);

index.generate();

for (var i=0; i<doc.stories.length; ++i) { // "...looping all stories of the document"
var story = doc.stories[i];
if (story.storyType == StoryTypes.INDEXING_STORY) {
// "...move texts[0] of that story to the first
// insertion point of the text frame in the last page"
doc.pages.lastItem().textFrames[0].insertionPoints[0].texts[0] = story.texts[0];

break;

}
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 22, 2020 Dec 22, 2020

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 )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 22, 2020 Dec 22, 2020
LATEST

That was it, thank you so much!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines