Skip to main content
Known Participant
August 5, 2009
Answered

(VBS CS2) Need indexing help: Creating a page reference

  • August 5, 2009
  • 1 reply
  • 1263 views

This is my first post on this forum. While I have years of experience in PageMaker scripting, I am new to InDesign scripting. I need to create a current page reference and index the word, "Widgets." I've read PDF's until my brain has turned to mush. I can't seem to connect the dots (literally) when it comes to this task.

This is my test script. The last line is where I am stuck:

Const idCurrentPage = 1668444263
Rem Hello World
Set myInDesign = CreateObject("InDesign.Application.CS2")
Rem Create a new document.
Set myDocument = myInDesign.Documents.Add

Rem Get a reference to the first page.
Set myPage = myDocument.Pages.Item(1)
Rem Create a text frame.
Set myTextFrame = myDocument.TextFrames.Add
Rem Specify the size and shape of the text frame.
myTextFrame.GeometricBounds = Array("0p0", "0p0", "18p0", "18p0")
Rem Enter text in the text frame.
myTextFrame.Contents = "This is the new WidgetPro."

Rem Create a new story.
Set myStory = myInDesign.ActiveDocument.Stories(1)
Rem Open the story editor.
Set myStoryEditor = myInDesign.ActiveDocument.Stories(1).StoryEdit
Rem Enter some text.
myStory.insertionPoints.item(-1).contents = vbCr
myStory.insertionPoints.item(-1).contents = "Widgets"
Rem Create an index (not sure if this is needed).
Set myIndex = myDocument.indexes.add()
Rem Add a topic (not sure if this is needed).
Set myTopic = myIndex.Topics.add("Widgets")
Rem Set a reference to the current page DOES NOT WORK
myTopic.pageReferences.add(myStory.insertionPoints[myIndex], PageReferenceType.CurrentPage)

A bit more info if it helps: This process of building the story with corresponding indexed words/phrases continues on through 30+ chapters totalling 500+ pages in the final book. The actual index is at the end of the book will be created through the normal InDesign menu commands.

-Ike

This topic has been closed for replies.
Correct answer Steven__

I finally figured out how to create an index, a topic and corresponding index marker. Here the woking code is for others who are seeking similar help:

Const idNo = 1852776480
Const idCurrentPage = 1668444263
Set myInDesign = CreateObject("InDesign.Application.CS2")
Set myDocument = myInDesign.Documents.Add
Set myPage = myDocument.Pages.Item(1)
Set myTextFrame = myDocument.TextFrames.Add
myTextFrame.GeometricBounds = Array("0p0", "0p0", "18p0", "18p0")
myTextFrame.Contents = "This is the new WidgetPro."
Set myStory = myInDesign.ActiveDocument.Stories(1)
Set myStoryEditor = myStory.StoryEdit
myStory.insertionPoints.item(-1).contents = vbCr
Set myIndex = myDocument.indexes.add()
Set myTopic = myIndex.Topics.add("Widgets")
myTopic.PageReferences.add myStory.InsertionPoints.Item(1), idCurrentPage
myStory.insertionPoints.item(-1).contents = vbCr

The line of code in blue was the part that was giving me fits. Please note the lack of surrounding parenthesis after '.add'. Considering I am new to InDesign scripting, I can only speculate this is a peculiarity of VBSEdit. If I try running the same script via InDesign's script palette, I get an error.

What is going on here? I need to get a handle on this and I still feel like I am coding by the million monkeys method.


As I replied earlier, in Classic VB there are Sub's and Functions. A Sub doesn't return anything, and it's parameters are not surrounded with parenthesis. A Function returns a value and its parameters are surrounded with parenthesis.

For example

Msgbox "This is a sub"

result = Msgbox("This is a Function")

even though the same command is used, it changes the rules when returning a value.

(Please note this wackyness has been changed in vb.net)

If you wan't to deal with a Sub as a Function, you can add the "Call" keyword before the Sub.

For example

Call Msgbox("This Sub acts like a Function")

Good Luck

Steven

1 reply

Steven__
Inspiring
August 6, 2009

I think the code is good, but VB arrays use parenthesis, and only functions put parameters in parnthesis. Use call to tur Sub into function

try

Call myTopic.pageReferences.add(myStory.insertionPoints.item(myIndex), PageReferenceType.CurrentPage)

Known Participant
August 6, 2009

I replaced my bad line of code with yours and get the error, Invalid parameter.

Maybe I am making it harder than what it needs to be. What I am trying to accomplish is the equivalent of selecting the word, Widgets, and hitting the keyboard shortcut, <Shift><Ctrl><Alt>[. I need the scripting equivalent of that.

[Edit] Does InDesign have the PageMaker equivalent of IndexAuto? [/Edit]

Known Participant
August 11, 2009

I finally figured out how to create an index, a topic and corresponding index marker. Here the woking code is for others who are seeking similar help:

Const idNo = 1852776480
Const idCurrentPage = 1668444263
Set myInDesign = CreateObject("InDesign.Application.CS2")
Set myDocument = myInDesign.Documents.Add
Set myPage = myDocument.Pages.Item(1)
Set myTextFrame = myDocument.TextFrames.Add
myTextFrame.GeometricBounds = Array("0p0", "0p0", "18p0", "18p0")
myTextFrame.Contents = "This is the new WidgetPro."
Set myStory = myInDesign.ActiveDocument.Stories(1)
Set myStoryEditor = myStory.StoryEdit
myStory.insertionPoints.item(-1).contents = vbCr
Set myIndex = myDocument.indexes.add()
Set myTopic = myIndex.Topics.add("Widgets")
myTopic.PageReferences.add myStory.InsertionPoints.Item(1), idCurrentPage
myStory.insertionPoints.item(-1).contents = vbCr

The line of code in blue was the part that was giving me fits. Please note the lack of surrounding parenthesis after '.add'. Considering I am new to InDesign scripting, I can only speculate this is a peculiarity of VBSEdit. If I try running the same script via InDesign's script palette, I get an error.

What is going on here? I need to get a handle on this and I still feel like I am coding by the million monkeys method.