optimization suggestions for my code for drawing a crossword grid

I typed previously of this little script I have to draw a crossword puzzle in InDesign CS 2. It takes supplied input (number of boxes across, number of boxes down, and what letters to fill it with) and uses it to draw the crossword to the size of a pre-selected rectangular object. Here’s the code:

on run {gridCols, gridRows, gridFill}

	-- gridCols is how many boxes across to draw the grid
	-- gridRows is how many boxes down to draw the grid
	-- gridFill consists of the letters to fill the grid with: letters A-Z, except for periods which are read as the bl;ack grid boxes

	tell application "Adobe InDesign CS2"
		activate

		if (count documents) > 0 then
			set myDoc to active document
			set mySelection to selection
			if (count mySelection) = 1 then

				set mySelectionBounds to geometric bounds of selection

				-- use the selected frame as the drawing area for the crossword grid

				set gridWidth to (item 4 of mySelectionBounds) - (item 2 of mySelectionBounds)
				set gridHeight to (item 3 of mySelectionBounds) - (item 1 of mySelectionBounds)

				set gridOriginX to (item 2 of mySelectionBounds)
				set gridOriginY to (item 1 of mySelectionBounds)

				set noOfCols to gridCols
				set noOfRows to gridRows

				set gridBoxWidth to (gridWidth / noOfCols)
				set gridBoxHeight to (gridHeight / noOfRows)

				tell active document

					set myGridAnswerCharStyle to character style "[None]"
					set myGridAnswerParaStyle to paragraph style "grid letters"
					set myGridAnswerBoxStyle to object style "grid letter box"
					set myGridAnswerBlackBoxStyle to object style "grid black box"

					repeat with rowNumber from 0 to (noOfRows - 1)

						repeat with columnNumber from 0 to (noOfCols - 1)

							set nextCharInGrid to ((rowNumber * noOfCols) + columnNumber + 1)

							set currentGridEntryLetter to (character nextCharInGrid of gridFill) -- this is the letter to draw in the grid box

							set myGridAnswerBox to make text frame with properties {geometric bounds:{(gridOriginY + (rowNumber * gridBoxHeight)), (gridOriginX + (columnNumber * gridBoxWidth)), (gridOriginY + ((rowNumber + 1) * gridBoxHeight)), (gridOriginX + ((columnNumber + 1) * gridBoxWidth))}, contents:currentGridEntryLetter}

							set thisTextFrame to object reference of myGridAnswerBox

							if currentGridEntryLetter = "." then -- a period means draw a black box

								set properties of thisTextFrame to {contents:""} -- delete the period in the text frame

								apply object style thisTextFrame using myGridAnswerBlackBoxStyle with clearing overrides

							else -- else it's a box with a letter in it to draw

								set properties of every paragraph of thisTextFrame to {applied character style:myGridAnswerCharStyle, applied paragraph style:myGridAnswerParaStyle}

								apply object style thisTextFrame using myGridAnswerBoxStyle with clearing overrides

							end if

						end repeat

					end repeat

					select nothing -- otherwise the last grid number box would still be selected

				end tell

			else if (count mySelection) > 1 then
				display dialog "Please select only one rectangular object to use for the grid drawing area."
			else
				display dialog "Please select a rectangular object to use for the grid drawing area."
			end if
		else
			display dialog "Please open a template, select a rectangular object to use for the grid drawing area, and try again."

		end if

	end tell

end run


Any general suggestions on how to optimize my code for more speed? Running the script from InDesign’s Scripts menu cuts the speed down to half, but I’d still like to get it a bit snappier.

No idea, unless you could find a way, for example, to create a “table” with all its attributes from scratch, instead of drawing every text box, or maybe writing some kind of XML which you could import later…

That’s actually an idea I should have given to joecab when he first mentioned this in another thread. DUH!
(I feel especially stupid since I’ve been teaching InDesign classes the last few weeks…)

Joecab, have you considered trying to use the Table feature of InDesign? I can’t give you ideas on how to script it, but if it is scriptable I bet you could create the entire table from scratch in a blink.

Can I assume you couldn’t find a way to script InDesign’s step-n-repeat functionality? Chances are if that exists it would be fast than the looping.

Like before, just some brainstorming ideas…unfortunately I’m in the middle of a big FileMaker project so can’t spend too much time digging more…

I actually thought of the table idea, but there are some instances where having separate boxes will be useful, so I ruled it out. I’m wondering if I should think about it again as an alternative. Can’t really use step & repeat much because the lines are the only parts placed regularly: the other boxes depend on the fill and have no set pattern. Thanks for the help, and I’ll think about it some more this weekend.