I am trying to develop a script that will place letter headings above the first occurrence of a new letter. The problem is, that I do not get how InDesign wants the new text to be called on. I always get an error message, saying InDesign can’t set contents of the paragraph in question. My problem is with the second part of the repeat, where A is not equal to B, etc.
If anyone has any helpful info, I greatly appreciate it.
tell application "Adobe InDesign CS3"
set myTextObjects to {text style range, paragraph, text column, text, story}
if (count documents) > 0 then
set mySelection to selection
if (count mySelection) > 0 then
if class of item 1 of mySelection is in myTextObjects then
my AddIndexHeadings()
else
display dialog "Text is not selected, or too little text is selected. Please select several paragraphs and try again."
end if
else
display dialog "Nothing is selected. Please select some text and try again."
end if
else
display dialog "No documents are open. Please open a document and try again."
end if
end tell
on AddIndexHeadings()
tell application "Adobe InDesign CS3"
tell document 1
try
set myParagraphStyle to paragraph style "Index section heading"
on error
--The paragraph style did not exist, so create it.
set myParagraphStyle to make paragraph style with properties {name:"Index section heading"}
end try
set mySelection to selection
set myText to item 1 of mySelection
repeat with i from 1 to (count paragraphs of myText)
if i = 1 then
set ParaB to (paragraph i of myText)
--set ParaBProps to properties of (paragraph i of myText)
--return ParaBProps
--return ParaB
set InitialChar to first character of ParaB
--return InitialChar
tell parent story of myText
set myPlaceText to InitialChar & return
set contents of insertion point 1 to myPlaceText
end tell
--return result
else if i > 1 then
set ParaA to (paragraph (i - 1) of myText)
set ParaB to (paragraph i of myText)
--return first character of ParaB
set InitialChar to first character of ParaB
if first character of ParaA ≠first character of ParaB then
set myPlaceText to InitialChar & return as text
tell paragraph ParaB of parent story of myText
set contents of ParaB to (myPlaceText & ParaB)
end tell
end if
end if
end repeat
end tell
end tell
end AddIndexHeadings