Can a table calculate a word count?
I write video scripts using tables. I would like to have a cell that displays the word count for a table column containing several cells with text. Is this possible?
I write video scripts using tables. I would like to have a cell that displays the word count for a table column containing several cells with text. Is this possible?
Hi Tracy,
Word count applies to the whole document or selected text. In Pages '09,
Six words. Now select the table
Four words in the table.
In Pages 5, it works for selected text, but not for a selected table . The only thing I can suggest is to copy the table and paste it into a new (temporary) document. Do the word count there.
Regards,
Ian.
It is not possible without AppleScript. The following was tested with Pages v5.2.2 on OS X 10.9.5.
The following AppleScript code will prompt you to input the table name, cell range, and cell to place the word count. Table names default to Table 1, Table 2, etc. It then will count all of the words in the specified cell range (A1:A4), and write the word count in the last (A5) cell. The prompt looks like this when filled:
I would copy/paste this AppleScript code in the AppleScript Editor/Script Editor, click the hammer icon to compile it, and then save it twice:
You double-click the application when you have an open Pages document with table(s) that can benefit from this code.
The following was my test table for this AppleScript code. There are 27 words, and the target (A5) cell has been updated with that count.
And the AppleScript:
tell application "Pages"
activate
try
set crange_target to the text returned of (display dialog "Enter data exactly as shown — table name,range,total cell" & return & "(e.g. Table 1,A1:A4,A5) :" default answer "")
on error errmsg number errnbr
-- Quit if user pressed cancel
if errnbr = -128 then
return
end if
end try
-- from the example, "Table, 1, A1, A4, A5"
set avector to words of crange_target
-- If you set a single name for a table in Pages (e.g. ThisHereTable)
-- you will need to adjust the code to only use item 1 and not item 2.
-- Correspondingly, items 3 and 4 for myRange would become items 2 and 3
-- and myTarget gets assigned item 4.
set tableName to (item 1 of avector) & space & (item 2 of avector)
set myRange to (item 3 of avector) & ":" & (item 4 of avector)
set myTarget to (item 5 of avector)
-- click replies in the AppleScript editor to see the contents
-- once you remove the comment characters
-- log (avector)
-- log (tableName)
-- log (myRange)
-- log (myTarget)
tell front document to tell table tableName
set cellwords to value of cells in range myRange
set wordcnt to 0
repeat with i from 1 to (length of cellwords)
set wordcnt to wordcnt + (count (words in item i of cellwords))
end repeat
-- log (cellwords)
-- log (wordcnt)
set value of cell myTarget to wordcnt
end tell
end tell
This is an update to my original post. Use this version. It does not error check user input. Extra points for instructions and typing accuracy. 😉 It requires an open Pages document containing the table you wish to act on. The selected table name can be revealed via checking Format panel: Table: Table Name.
Whats new
Not because I don't know how to do this in AppleScript, but because no matter what, it would not work, despite textbook AppleScript
As before, copy/paste the following AppleScript code into the AppleScript Editor/Script Editor located in /Applications/Utilities. Do the following:
AppleScript code:
-- An AppleScript solution that will count all words in a named table, its cell range, and place
-- the word count in a specified target cell.
--
-- Apple Support Community Link: https://discussions.apple.com/thread/6853181
-- Tested: Mavericks 10.9.5 (Pages v5.2.2), Yosemite 10.10.2 (Pages v5.5.2), default Ruby
-- Version: 1.2
-- Author: VikingOSX, 1 March 2015, Apple Support Community (Pages)
try
set crange_target to the text returned of (display dialog "Enter data exactly as shown — table name, range, total cell" & return & "(e.g. Table 1, A1:A4, A5) :" default answer "")
on error errmsgnumbererrnbr
-- Quit if user pressed cancel
if errnbr = -128 then
return
else
display dialog "[ " & errnbr & " ]: " & errmsg
end if
end try
set avector to paragraphs of rsplit(crange_target) as list
-- log (avector)
set tableName to avector'stext item 1
set myRange to avector'stext item 2
set myTarget to avector'stext item 3
-- log (tableName)
-- log (myRange)
-- log (myTarget)
tell application "Pages"
activate
tell front document to tell table tableName
set cellwords to value of cells in range myRange
set wordcnt to 0
repeat with i from 1 to (length of cellwords)
set wordcnt to wordcnt + (count (words in item i of cellwords))
end repeat
set value of cell myTarget to wordcnt
end tell
end tell
on rsplit(astring)
-- AppleScript will reformat this code on Mavericks, but not on Yosemite
-- The last line of Ruby, in this case puts, is an implicit return
do shell script "/usr/bin/ruby <<'EOF' - " & quoted form of astring & "
#encoding: utf-8
str = ARGV[0].to_s
tbl,rge,dest = str.split(', ')
puts tbl, rge, dest
'EOF'"
end rsplit
Thanks. I have been doing the paste-table-into-empty-doc dance and it works fine.
Sure wish there were a table macro for calculating words.
Wow, thanks for all the Apple-scripting!
Can a table calculate a word count?