> On Nov 5, 2015, at 9:42 , Nobumi Iyanaga <[log in to unmask]> wrote:
> 
> 2. The second macro would be much more complicated. Here is the situation. I have files for several chapters (one file for each chapter), which are being written and will be finished successively. As the deadline is very soon, I began to work to create indices for each file, beginning with chapter one. These indices being done, I have to create indices for the next files (and finally join all the files, to make big a big file and big indices). To make coherent indices, I have to use those for the first chapter, that is take the same topics, and find the same words, and make indices of the subsequent chapter files with them -- and after that, add other words under other topics for these files...
> 
> So the macro should take a list of topics (which would be the selected text), and make a list of all the topics, and all the words, etc. associated with each of these topics in a file. Ideally, the result should be a file with a table of two columns, that would look like the following:
> 
> 	words, etc.			topics
> _____________________________________________________
>  word1|word2|chunk_of_string3		topic
> 		etc.			etc.
> 
> When such a file would be created, I would be able to examine each entry, and make a list of words and topics, so that I would be able to index these words in the new chapter file, at once, with the macro "Index Using Word List (PowerfindPro)" that Philip created for me...
> 
> Would it be possible to write such a macro?
> 


Sure.

# Macro Make Word Lists from Index Topics
# v.1.0  2015/11/5
#

$doc = Document.active

# Prepare a hash for all the indexes
$indexes = Hash.new

# Check all the index styles
$indexNames = Text Index Names
foreach $indexName in $indexNames

# Prepare a hash for all the topic for a given index
$indexTopics = Hash.new

# Go through all the texts of the document and look for indexed items
foreach $text in $doc.allTexts
$loc = 0
while $loc < $text.length
$range = $text.rangeOfAttributesAtIndex $loc
$topics = $text.attributesAtIndex($loc).textIndexTopicsForStyleName($indexName)

# when we find an indexed item
if $topics

# save the selection of the indexed topic in the hash
foreach $topic in $topics
$sel = TextSelection.new($text,$range)
if $indexTopics.definesKey($topic)
if $indexTopics{$topic}.lastValue.bound == $sel.location
$prev = $indexTopics{$topic}.pop
$sel = $prev.unionKissingSelection $sel
end
$indexTopics{$topic}.push $sel
else
$indexTopics{$topic} = Array.new $sel
end
end

end
$loc = $range.bound
end
end

# save the selection hash for the given index
$indexes{$indexName} = $indexTopics
end

# generate output word list tables
# one for each index
foreach $indexName in $indexNames
$list = Array.new
foreach $topic in $indexes{$indexName}.keys
foreach $sel in $indexes{$indexName}{$topic}
if $topic.count
$indexAs = Cast to String $topic
$indexAs.replaceAll '", "', ':'
$indexAs.replaceAll '^\("', '', 'E'
$indexAs.replaceAll '"\)$', '', 'E'
else
$indexAs = '—'
# $indexAs = $sel.substring
end
$list.push $sel.substring & "\t" & $indexAs
end
end
$list.sort
$listDoc = Document.newWithText $list.join("\n") & "\n"
$listDoc.text.replaceAll '^(.+\n)\1+', '\1', 'E'
Select All
Convert to Table
end

Philip Spaelti
[log in to unmask]