Can't replace text in InDesign text frame

I have a text frame on a master page that I wish to unlock and replace the content.

The master page frame has two paragraphs using two separate paragraph styles. The first para says “[Chapter Title]”, the second says “[Chapter subtitle]”.

I have a script that applies a master spread, successfully unlocks a frame from the master, and replaces the first paragraph of text “ but I get an error on the second para. Here’s the script:

set chapHeadText to "New chapter title" as string
set chapSubText to "New chapter subtitle"

tell application "Adobe InDesign CS5.5"
	tell active document
		set activePage to page 3
		set applied master of activePage to master spread "B-Chapter Starts" -- Apply master
		override text frame 1 of master spread "B-Chapter Starts" destination page activePage -- Unlock text frame
		set unlockFrame to text frame 1 of activePage
		tell parent story of unlockFrame
			set paragraphOne to object reference of text from character 1 to character -2 of paragraph 1
			set paragraphTwo to object reference of text from character 1 to character -2 of paragraph 2
			set contents of paragraphOne to chapHeadText
			set contents of paragraphTwo to chapSubText
		end tell
	end tell
end tell

I’ve tried various different methods to access para 2, but always get an error. For the above, I get: Adobe InDesign CS5.5 got an error: Can’t set text from character 17 to character 35 of story id 485 of document id 83 to “New chapter subtitle”.

What am I doing wrong? I tried to find a description of ‘insertion points’ to see if I could use those, but can’t find any reference on them. I have the As scripting guide from Adobe, but that’s pretty slim on information when it comes to most of the problems I’ve encountered.

Any help appreciated.

Model: MacPro Qaud Xeon
AppleScript: 2.4
Browser: Safari 5.1 (7534.48.3)
Operating System: Other

Hi. When calling a specific range, all of it must exist. Your script seems to be modifying a character that was possibly deleted in the set paragraphOne statement.

Okay, thanks Marc.

How can I get the correct insertion point? Do I have to explicitly reference a character range in the complete story, or can I use paragraph to select the second para?

As far as I understand it (which isn’t too far!), character 1 to character -2 references the first character to the penultimate character (the new paragraph character). This is so that the applied styles remain intact.

Maybe I should just make a nested style that switches to the subtitle style when it reaches the new paragraph symbol. Seems like a bit of a workaround though.

Well, I’ve tried using paragraph 1 and paragraph 2, and whilst the first paragraph substitution works, the second doesn’t.

Can anyone explain why I can’t simply set contents of paragraph 2 in the same manner?

I’ve tried various random methods including object reference of text, explicit character ranges (even though I can’t find how to reference these fully), but no joy. And I still haven’t found anywhere that references the parts of paragraph that can be addressed (i.e., insertion point 1 or -1 etc.)

I find it hard to believe that such a seemingly simple, basic operation is so ridiculously obscure!

Okay … after a bit of fettling, I finally got it to work.

I had to delete the contents of the text frame first, then set the contents again, applying styles as I went. I tried delete … replacing with …, but that didn’t work either, so here’s the new, working code (so far!)

tell application "Adobe InDesign CS5.5"
	tell active document
		set activePage to page 3
		set applied master of activePage to master spread "B-Chapter Starts" -- Apply master
		override text frame 1 of master spread "B-Chapter Starts" destination page activePage -- Unlock text frame
		set unlockFrame to text frame 1 of activePage
		tell parent story of unlockFrame
			delete paragraphs
			set contents of insertion point 1 to chapHeadText -- variable setup earlier
			set applied paragraph style of paragraph 1 to "Chapter Title"
			set contents of insertion point -1 to return
			set contents of insertion point -1 to chapSubText -- variable setup earlier
			set applied paragraph style of paragraph 2 to "Chapter Subtitle"
		end tell
	end tell

Hope that helps someone!