InDesign CS index scripting.

Hello everyone. I need a script that will take the text items in a .txt file separated by returns and for each of them create a page reference in the index. The page references need to be applied to all occurrences of that text though.

So, this is what I have so far…


set theFile to (choose file with prompt "Choose a tab-delimited text file") as string
set topicList to read file theFile using delimiter {return}

tell application "InDesign CS"
	repeat with i from 1 to number of items in topicList
		set currentTopic to item i of topicList as text
		
	end repeat
end tell


now I have no idea what context to use to try and create the page references. All they need is to exist and show the page numbers of all occurrences, no sub-topics, anything like that. Can anyone help? Thanks.

anyone?

This should help you get started, it will return a list of pages that the text appears in, in this case one word. Duplicate pages are eleminated with the if statement so that page 1 does not appear 10 times if the word appears on that page multiple times.

set IndexText to "president"

tell application "InDesign CS"
	set find preferences to nothing
	set find text of find preferences to IndexText
	set MyFoundItems to search document 1 with find preferences
	set PageList to {}
	repeat with AnItem in MyFoundItems
		set CurrentItemPage to 0
		set CurrentItemPage to name of parent of parent text frame of AnItem
		if CurrentItemPage is not in PageList then set PageList to PageList & CurrentItemPage
	end repeat
end tell
return PageList

Thats awesome, but unfortunately I just need to be able to find the correct syntax to be able to actually create the index topics and page references. Our book is unique being that we want every occurrence of the word to show, so just turning on the “add all” function when creating the page-references would do it for me.


tell application "InDesign CS"
	set find preferences to nothing
	tell document 1
		set myIndex to make index
		repeat with i from 1 to number of items in topicList
			set currentTopic to item i of topicList as text
			tell myIndex
				try
					set myTopic to index topic currentTopic of myIndex
				on error
					set myTopic to make topic with data {currentTopic}
				end try
			end tell
			
		end repeat
	end tell
end tell

This is what I have. It errors out at “make topic with data {currentTopic}”. It says it can’t make class topic. I am assuming the terminology in InDesign is different in CS than it was in 2.0.2 where this script supposedly worked.

I have to admit I have never used InDesign’s indexing, so I don’t know how it works. That said you need to change:

set myTopic to make topic with data {currentTopic}

to:

set myTopic to make topic with properties {name: currentTopic}

Okay, so the script below gives me a list of topics pulled from my Tab-Delimited text file. Great, the problem is that a topic list is useless without page references, and there seems to be no way to create those in InDesign via a script. Can anyone help, please? The page reference section in my InDesign script dictionary shows up as a class with properties just like “Topic” does… but when I try and make a page reference in the same way I make the topics it says “Cannot make class page reference”.

set theFile to (choose file with prompt "Choose a tab-delimited text file") as string
set topicList to read file theFile using delimiter {return}

tell application "InDesign CS"
	set find preferences to nothing
	tell document 1
		set myIndex to make index
		repeat with i from 1 to number of items in topicList
			set currentTopic to item i of topicList as text
			tell myIndex
				try
					set myTopic to index topic currentTopic of myIndex
				on error
					set myTopic to make topic with properties {name:currentTopic}
				end try
			end tell
			
		end repeat
	end tell
end tell