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?

Posted on Feb 28, 2015 12:27 PM

Reply
5 replies

Feb 28, 2015 7:56 PM in response to Tracy E

Hi Tracy,

Word count applies to the whole document or selected text. In Pages '09,

User uploaded file

Six words. Now select the table

User uploaded file

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.

Feb 28, 2015 8:15 PM in response to Tracy E

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:


User uploaded file


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:

  1. As an AppleScript text file with the first save. Do not hide the extension.
  2. As an AppleScript application (with extension hidden) using option+Save As… on your Desktop.


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.


User uploaded file


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

Mar 1, 2015 7:34 AM in response to Tracy E

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

  1. Allowed whitespace in user input dialog
  2. Reduced AppleScript complexity
  3. Uses Ruby-based, AppleScript function to split input fields into a list

    Not because I don't know how to do this in AppleScript, but because no matter what, it would not work, despite textbook AppleScript

  4. Faster script


As before, copy/paste the following AppleScript code into the AppleScript Editor/Script Editor located in /Applications/Utilities. Do the following:

  1. Click the toolbar hammer to compile the script
  2. Save twice
    1. As AppleScript text without extensions hidden to table-wrdcnt.applescript (the name is your choice)
    2. Option+Save As… as AppleScript application with extensions hidden to the Desktop as table-wrdcnt.app
  3. With an open Pages document, you double-click the table-wrdcnt application, and enter content using the instruction guideline.


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

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Can a table calculate a word count?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.