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.