Guest
2010-06-25, 08:36 AM
An updated 003 word-count script which:
- Gives separate and aggregated word counts for all rich text columns (and notes)
- Reports separate word counts for: all rows, visible rows, and selected rows
- Offers, in the report dialog, to copy the report into the clipboard (with timestamp and filename)
(The distinction between visible and invisible rows makes it possible to get word-counts for particular sections or sub-sections selected in the utility panel, and to get differing word counts for various levels of outline collapse)
(As with the previous draft, this approach of getting lists of property values from a reference yields much faster word counts than the iterative scripts first posted in 2008)
Code:
-- Ver 0.7
-- Gives word count for: all rows, visible rows, selected rows,
-- and for each rich text column (and notes), as well as totals
-- Report dialog offers option of copying report to clipboard
-- (with datestamp and filename)
property pstrOfferCopy : "Copy report to clipboard"
property pDelim : space
property pEOL : return & return
on run
tell application id "com.omnigroup.OmniOutlinerPro3"
try
activate
set oDoc to front document
on error
return
end try
-- Ensure that text item delimiter is SPACE
-- and prepare to restore any other value at the end of the script
set strDelim to my text item delimiters
set my text item delimiters to pDelim
tell front document
-- DEFINE SETS OF ROWS TO REPORT ON
-- All rows by default
set refAllRows to a reference to rows
set lngRows to count of refAllRows
set lstRowSets to {{"all", refAllRows}}
-- Plus visible rows if any are hidden
set refVisblRows to a reference to (rows where visible is true)
set lngVisible to count of refVisblRows
if lngVisible is not lngRows then ¬
set end of lstRowSets to {"visible", refVisblRows}
-- Plus selected rows if any are selected
set refSeldRows to a reference to selected rows
set lngSeldRows to count of refSeldRows
if lngSeldRows > 0 then ¬
set end of lstRowSets to {"selected", refSeldRows}
-- ASSEMBLE A LIST OF ALL RICH TEXT FIELDS (INCLUDING NOTES)
set refCols to a reference to (columns where type is rich text)
set lstColNames to name of refCols
if first item of lstColNames is "" then set first item of lstColNames to "Notes"
set lstColIDs to id of refCols
set lngCols to length of lstColIDs
-- COLLECT THE WORD COUNTS FOR ROW SETS & COLUMNS, WITH TOTALS
set lstData to {}
repeat with iRowSet from 1 to length of lstRowSets
set {strSetName, refSet} to item iRowSet of lstRowSets
set lstSet to {{strSetName, count of refSet}}
set intSetWords to 0
repeat with iCol from 1 to lngCols
-- Count words in this column
set strId to item iCol of lstColIDs
set lstColText to value of cell id strId of refSet
set intColWords to count of words of (lstColText as text)
-- Record the column name and word count
set end of lstSet to {item iCol of lstColNames, intColWords}
-- Increment the total word count
set intSetWords to intSetWords + intColWords
end repeat
set end of lstSet to {"total", intSetWords}
set end of lstData to lstSet
end repeat
end tell
end tell
-- BUILD A LEGIBLE REPORT FOR THE USER
set strMsg to ""
set intCols to (length of (first item of lstData)) - 1
-- A REPORT FOR EACH SET OF ROWS
repeat with iRowSet from 1 to length of lstData
set lstSetData to item iRowSet of lstData
set {strSetName, lngRows} to first item of lstSetData
set lngTotal to last item of last item of lstSetData
set strMsg to strMsg & lngTotal & pl(lngTotal, "word") & " in " & strSetName & " rows" & tab & "(" & lngRows & " " & pl(lngRows, "row") & ")" & return
-- WITH A SUB-REPORT FOR EACH RICH TEXT COLUMN (INCLUDING NOTES)
repeat with iCol from 2 to intCols
set {strColName, intColWords} to item iCol of lstSetData
set strMsg to strMsg & tab & strColName & ": " & intColWords & pl(intColWords, "word") & return
end repeat
set strMsg to strMsg & return
end repeat
set my text item delimiters to strDelim
tell me
activate
set recResponse to display dialog strMsg buttons {pstrOfferCopy, "OK"} with icon 1 with title ("WORD COUNT")
if button returned of recResponse is pstrOfferCopy then ¬
tell application id "com.apple.finder" to set the clipboard to ((current date) as string) & return & name of oDoc & return & return & strMsg
end tell
end run
-- Pluralize word unless lngQuant is 1
on pl(lngQuant, strWord)
if lngQuant is 1 then return " " & strWord
" " & strWord & "s"
end pl
Last edited by RobTrew; 2010-06-25 at 10:46 AM..