[Date Prev][Date Next] [Thread Prev][Thread Next]
[Date Index] [Thread Index] [New search]

Re: AppleScript for Word Count



>>I am hoping to get some help from anyone out there familiar with 
AppleScript.
>  A call to arms!!!

Lock and load!

>>... I ripped off / came up with a script that only gave me the 
>>count for text Flow 1. Underwhelming.
>
> Actually, it's probably all you want. Typically, unless you've 
> changed something, text flow 1 is the one named flow "A", and is the 
> main body text flow.

I strung this script together out of several small
demo scripts. It will give you the number of words in
text flow 1, including tables. Extending it to count
other text flows should be fairly easy, although that
shouldn't be necessary in most cases.

I've inlined a bunch of discussion as comments, so you
should be able to simply cut&paste into a text editor
if the line wrapping doesn't screw everything up....


tell application "FrameMaker 7.0"
        set theDoc to (active document)
        set mainFlow to (text flow 1 of theDoc) as reference

(* Counting words in a text flow doesn't require
adding up each individual paragraph, at least in
my experience. Use shortcuts if you can; the script
runs a bit faster. *)
        set totalWords to (count words of mainFlow)
 

(* Dealing with tables is definitely a hair-
pulling exercise. You can't say "tables of text
flow 1" unfortunately, and there are lots of
tables on reference pages... so you can't say
"tables of document" either.

So, we loop through each table and build a list
of tables that actually appear in text flow 1. *) 
        set theTabs to (tables of theDoc) -- all tables
        set tabLocs to {} -- the tables we want
 
        repeat with oneTab in theTabs
 
                set x1 to (location of oneTab)

(* A location looks like 'beginning of paragraph 1
of text flow 1 of document "foo:bar"' -- if they're
in a flow. But not all table locations look like
that, so we can't just say "container of container"
to strip the location down to the text flow; we have
to check before we strip the second container off. *)
                set tabCont to (container of x1)
 
                if class of tabCont is not text flow then
                        set tabCont to (container of tabCont)
                end if
 
(* Now we can compare text flows & add the table to
the list of tables to check if need be. *)
                if (tabCont is mainFlow) then
                        set end of tabLocs to oneTab
                end if
 
        end repeat

(* Now we can count the words in each cell of each
table. Here's were you have to get down to specifics;
you can't say "count words of table 1" unfortunately.
Watch the line wrap & correct if necessary. *)
 
        repeat with theTab in tabLocs
                repeat with theRow from 1 to num rows of theTab
                        repeat with theCol from 1 to num columns of theTab
                                set totalWords to totalWords + (count words of text of cell theCol of row theRow of theTab)
                        end repeat
                end repeat
        end repeat

(* Finally, we can enjoy the fruits of our labor: *) 
        display dialog ((totalWords as string) & " words in document.")
 
end tell

One of the things you've probably noticed is that I
tend to operate on the "active document" instead of
prompting for a file. Each approach has its advantages;
it just depends on the way you use your scripts. One
possible variation would be to rewrite the script as
a drag&drop applet, testing to see whether you drop
a book or a document: if you drop a book on it, the script
could open each book component and generate a word
count for the entire book.

One of these days, I need to see what I can make
AppleScript Studio do....

--
Larry Kollar, Senior Technical Writer, ARRIS
"Content creators are the engine that drives
value in the information life cycle."
    -- Barry Schaeffer, on XML-Doc


** To unsubscribe, send a message to majordomo@omsys.com **
** with "unsubscribe framers" (no quotes) in the body.   **