February 13, 2007

Applescript and Accordance to fill a Word table

I've had some questions about how I use something like Applescript to add full scripture texts to a table of scripture references. Instead of talking about it, I'll just show you a script to do that very thing. I've added comment lines that describe what most of the lines do. The scriptability of Microsoft Word 2004 is phenomenal.

-- This script cycles through a table of scripture references and looks up the text in Accordance, adding the full text to the end of the cells.

-- It works on the first table of the active document in Microsoft Word.

tell application "Microsoft Word"

-- work on the first table of the active Word document

set theTable to table 1 of active document

set rowCount to (count rows of theTable)

set columnCount to (count columns of theTable)

-- speed things up by not redrawing the window after each retrieval

set screen updating to false

-- repeat the functions with every cell of every row of every column

repeat with rr from 1 to rowCount

repeat with cc from 1 to columnCount

-- you can change those two lines to work on only certain rows or columns, such as:

-- repeat with cc from 3 to 5

-- will restrict it to columns 3, 4 and 5.

-- set the current working range to the next cell

set aCell to cell cc of row rr of theTable

set aRange to create range active document start (start of content of text object of aCell) end ((end of content of text object of aCell) - 1)

set theRef to content of aRange

-- empty cells have a ~, indicating move on to next cell

if theRef does not contain "~" then

-- try to get the scripture text from Accordance


-- change the module name from "GNT" to whatever you wish.

-- the result is in the custom citation format of Accordance prefs

tell application "Accordance" to set theText to «event AccdTxRf» {"GNT", theRef, true}

-- set the current range to the end of the contents of current cell

set aRange to create range active document start ((end of content of text object of aCell) - 1) end ((end of content of text object of aCell) - 1)

-- add the results to the end of the cell after a carriage return

set content of aRange to return & theText

-- redefine the range to inclue the text we just placed in the cell

set aRange to move start of range aRange by a character item count 1

set aRange to change end of range aRange by a cell extend type by selecting

-- do whatever formatting you'd like to the inserted text, otherwise, it will have the character format already present in the cell

set style of aRange to "GreekText"

-- set bold of font object of aRange to false

-- set color index of font object of aRange to black

end try

end if

end repeat

end repeat

-- ok, now update the window of the Word document

set screen updating to true

end tell


Les said...

I'd like to do something like this, but a little different. I'd like to create a script or Automator Action that would do the following:

*Take a list of scripture references from a standard Word doc, not a table.

*Look up those references in the Bible Version of my choosing

*Spit all those verses out in Accordance Citation format

*Save the resulting file as a plain text file

*Open the text file in OmniOutliner

*Export the file in Keynote format

*Open the Keynote presentation for editing

Do you think that's a possiblity?

Joe Weaks said...

Les, you may want to try email for further help, but this should get you started as a script. The interim step of creating a text file is unnecessary... you can have the script create items in OmniOutliner directly:

-- Get the paragraphs from the front document of Word.
-- Assumes they're each a scripture reference.
tell application "Microsoft Word" to set theReferences to content of text object of active document
set theReferences to paragraphs of theReferences

-- Creates a new OmniOutliner document
tell application "OmniOutliner" to make new document at end of documents

-- Or this version would have it create a new document only if one didn't already exist
--tell application "OmniOutliner" to if not (exists document 1) then make new document at end of documents

set theResult to ""

-- Repeat for each paragraph, look it up in Accordance and then create a new row in the OO front document.
-- Change "NRSV" to any text module.
repeat with thisReference in theReferences
tell application "Accordance" to set thisResult to «event AccdTxRf» {"NRSV", thisReference, true}
tell application "OmniOutliner"
set r to make new row at the end of children of document 1
set topic of r to thisResult
end try
end repeat

-- I don't know OmniOutliner so don't know correct syntax for the script line to export a file to Keynote
set n to (get path to desktop folder as string) & "test.key"
set n to alias n
tell application "OmniOutliner" to save document 1 as "Keynote" in (alias n)

Joe Weaks said...

Note that the garbled brackets around «event AccdTxRf»
are the guillerments, Option \ and Option Shift \
«event AccdTxRf»