Finder - Word count

Discussion in 'Mac Apps and Mac App Store' started by Reviresco, Apr 17, 2010.

  1. Reviresco macrumors newbie

    Joined:
    Feb 13, 2010
    #1
    Hi,

    Im wondering whether its at all possible to display word count as one of the columns in finder or if there is a program that will let me do this?

    Basically in compiling a dissertation spread over 40 or so words docs and i need to be able to keep a running tab on word count. Continually putting them all into one big word doc just to grab a total is very time consuming.

    Any suggestions appreciated.

    Rev
     
  2. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #2
    What if you had an AppleScript that would count the words in a file, then save them to the file's Spotlight comments?

    Then you'd make sure comments were visible in that folder.

    What word processor are you using?

    mt
     
  3. codymac macrumors 6502

    Joined:
    Jun 12, 2009
    #3
    Are you comfortable at the command line? If you can strip the Word formatting, it will be much easier.

    This Perl script will do that and output ascii files with the same file names:

    http://docx2txt.sourceforge.net/

    From there, it's simply a matter of running a wc -w on the ascii files to get the word count. Ex:

    ./docx2txt.sh body_text.docx
    wc -w body_text.txt

    I'd probably just take the Perl script and use it as a basis for creating a shell script that would run against a directory and output file count, line count, and word count. That way I could run a single command and by the time I topped off my coffee the results would be on my screen.

    It should go without saying... but backup your stuff before running anything against it, just in case.
     
  4. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #4
    I did some experimenting and this setup works pretty well.

    First you need to collect all your files in a specific folder.

    That folder must have comments visible. (Right-click the folder, select View Options, check "Comments")

    Next, you'll need to save this script in your /Library/Scripts/Folder Action Scripts/ folder.

    Here's the script:

    Code:
    on adding folder items to this_folder after receiving added_items
    	tell application "Finder"
    		set fold_name to the name of this_folder
    		try
    			repeat with i from 1 to number of items in added_items
    				set new_item to item i of added_items
    				if name extension of new_item is "pages" then
    					tell application "Pages"
    						open new_item
    						set wc to the count of words of document 1
    						close document 1
    					end tell
    					if wc > 0 then
    						set comment of new_item to "Word count: " & (wc as string) & return
    					end if
    				end if
    				-- display dialog "Added: 'Word count: " & wc & "'"
    			end repeat
    		on error errMsg number errNum
    			display dialog errMsg & return & "Error: " & (errNum as string)
    		end try
    	end tell
    end adding folder items to
    
    
    on opening folder this_folder
    	tell application "Finder"
    		set fold_name to the name of this_folder
    		try
    			set all_items to items of this_folder
    			repeat with i from 1 to count of all_items
    				set new_item to item i of all_items
    				if name extension of new_item is "pages" then
    					tell application "Pages"
    						open new_item
    						set wc to the count of words of document 1
    						close document 1
    					end tell
    					if wc > 0 then
    						set comment of new_item to "Word count: " & (wc as string) & return
    					end if
    				end if
    				-- display dialog "Added: 'Word count: " & wc & "'"
    			end repeat
    		on error errMsg number errNum
    			display dialog errMsg & return & "Error: " & (errNum as string)
    		end try
    	end tell
    end opening folder
    After you've saved the script, you'll need to configure the folder to accept folder actions. Right-click the folder and the pop-up menu should include "Folder Actions Setup..." Window should pop up, then a panel should slide from the title bar. If you've saved the script in the right folder, it should appear and just select it. Then quit the Folder Actions Setup app.

    I used Pages as the word processor. If you're using Word, I'm sure the syntax is similar.

    Basically, if you copy a file into your work folder, the script opens it, gets the word count, then closes it. It then sets the Spotlight comment to "Word count: xxx"

    The second half of the script is an easy way to update the word count after you've edited a file. Folder Actions don't seem to recognize when a file has changed. If you've edited a file, just close and open the folder and the script will run through all the files. If you've got a lot, it might be painful; maybe there's an alternative. But if you only have a few, this script runs pretty quick.

    mt
     

Share This Page