A simple MacOS daily word count automator

Hey folks!

****** UPDATE ******

NOW WORKS WITH SCRIVENER !!

Long time no update. Music Monday and Photo Fridays will be returning. I’d mostly just forgotten how far out I scheduled them and kept forgetting to set up more. But that’s not what this post is about, so we’ll get to that later.

At the end of every month I post my accountability update (a monthly view of the word tracker which I got from http://inkhaven.net/2017/01/2017-word-count-tracker-is-here/). This last time, a few folks mentioned wanting to track their daily counts automatically, and since I’ve been getting into AppleScript again for unrelated reasons, I decided to take a stab at it.

Unfortunately for some, this is going to be geared to me. I do my rough draft work in Pages, and my drafting / cleanup in Scriviner. Scrivener has a great daily word tracking function anyway, so this is just figuring out session/daily counts.

First off, you’ll need the file for tracking. Again, tailored to me, so this is set up for Numbers because it’s free with every Mac and easier to use AppleScript on than Google Sheets. Here’s a sample file, but really you just need to have something that looks along the lines of the following:

Screen Shot 2017-09-03 at 2.11.13 PM

Update the file to have the latest word count of any WiP you want to track with “Pre-Tracking” as the thing under date. Save it with the changes to an easy to find spot on your hard disk.

Once you have your file saved, take note of the location for later.

Now, open up ScriptEditor. If you’ve never used it before, easiest thing to do is use system search (command+spacebar) to find it. A new script opens up and you can just copy the grey block text at the end of this post into your script, changing the red text to the path of your numbers file.

Once you’ve got it set up, save it somewhere easy to find. Whenever you finish a writing session, simply run the script. It will open your tracking sheet, add a new line for this session, track the words you wrote, and update your total for the day.

set theDate to date string of (current date)

-- Pages and Scriviner are only currently supported apps. 

-- Remove if you don't use one. 

set myApps to {"Pages", "Scrivener"}

-- Pop up list if you switch between apps (comment out with -- if you don't need)

set UserApplicationsChoice to choose from list myApps with title "Application Dialog" with prompt "What's applications should I check?" default items "" with multiple selections allowed

-- Hard code list if you always use the same apps (uncomment to run)

-- set  UserApplicationsChoice = myApps

log UserApplicationsChoice

-- Goes through each app to update the wordcount 

repeat with a from 1 to count of UserApplicationsChoice

if item a of UserApplicationsChoice = "Pages" then

set script_parts to check_wc_pages()

set FileName to item 1 of script_parts

set WordCounts to item 2 of script_parts

update_the_spreadsheet(WordCounts, FileName, theDate)

end if

if item a of UserApplicationsChoice = "Scrivener" then

set script_parts to check_wc_Scriviner()

set FileName to item 1 of script_parts

set WordCounts to item 2 of script_parts

update_the_spreadsheet(WordCounts, FileName, theDate)

end if

end repeat

on check_wc_pages()

tell application "Pages"

set docName to name of front document

tell front document to set wcount to (count of words of body text)

return {docName, wcount}

end tell

end check_wc_pages

on check_wc_Scriviner()

tell application "System Events" to tell process "Scrivener"

-- Set Focus on Scrivner to allow script to work

set frontmost to true

delay 1

try

click menu bar item "Project" of menu bar 1

click menu item "Hide Project Targets" of menu 1 of menu bar item "Project" of menu bar 1

end try

-- *** Get the name of the current WiP File *** 

-- Click the “File” menu.

click menu bar item "File" of menu bar 1

-- Save As...

delay 1

click menu item "Save As..." of menu 1 of menu bar item "File" of menu bar 1

-- Get Current Filename 

repeat with a from 1 to count of windows

try

set docName to value of text field 1 of sheet 1 of window a

log docName

-- exit menu

click UI element "Cancel" of sheet 1 of window a

end try

end repeat

-- *** Get total wordcount of WiP File ***

click menu bar item "Project" of menu bar 1

click menu item "Show Project Targets" of menu 1 of menu bar item "Project" of menu bar 1

set wcount to value of static text 2 of window 1

log wcount

return {docName, wcount}

end tell

end check_wc_Scriviner

on update_the_spreadsheet(wcount, docName, theDate)

tell application "Finder" to open "Macintosh HD:Users:fredyost:Documents:WordCountSample.numbers"

delay 5

set filenames to {}

set WordCounts to {}

set dateNames to {}

set Dailywordcounts to {}

tell application "Numbers"

tell front document

tell table 1 of sheet 1

set fullSheet to 2

repeat

try

set cellValue to value of cell ("A" & fullSheet)

on error

add row below row (fullSheet - 1)

set cellValue to value of cell ("A" & fullSheet)

end try

set cellValue to value of cell ("A" & fullSheet) --of table 1 of sheet 1

set WordCount to value of cell ("C" & fullSheet)

log cellValue & ":" & WordCount

if cellValue is not in filenames and cellValue is not missing value then

set end of filenames to cellValue

set end of WordCounts to WordCount

else

repeat with a from 1 to count of filenames

if item a of filenames is cellValue then

set item a of WordCounts to WordCount

end if

end repeat

end if

if cellValue = missing value then

if docName is not in filenames then

set value of cell ("A" & fullSheet) to docName

set value of cell ("B" & fullSheet) to theDate

set value of cell ("C" & fullSheet) to wcount

set value of cell ("D" & fullSheet) to wcount

else

repeat with a from 1 to count of filenames

if item a of filenames is docName then set prevwcount to item a of WordCounts

end repeat

if (wcount-prevwcount) > 0 then

set value of cell ("A" & fullSheet) to docName

set value of cell ("B" & fullSheet) to theDate

set value of cell ("C" & fullSheet) to wcount

set value of cell ("D" & fullSheet) to (wcount - prevwcount)

end if

end if

exit repeat

end if

set fullSheet to fullSheet + 1

end repeat

end tell

end tell

end tell

tell application "Numbers"

tell front document

tell table 1 of sheet 1

set fullSheet to 2

repeat

try

set workingDate to value of cell ("B" & fullSheet)

on error

add row below row (fullSheet - 1)

set workingDate to value of cell ("B" & fullSheet)

end try

set WordCount to value of cell ("D" & fullSheet)

if workingDate is not in dateNames and workingDate is not missing value then

set end of dateNames to workingDate

set end of Dailywordcounts to WordCount

else

repeat with a from 1 to count of dateNames

if item a of dateNames is workingDate then

set item a of Dailywordcounts to (WordCount + (item a of Dailywordcounts))

end if

end repeat

end if

if workingDate = missing value then exit repeat

set fullSheet to fullSheet + 1

end repeat

set fullSheet to 2

repeat

set workingDate to value of cell ("B" & fullSheet)

if workingDate is not missing value then

repeat with a from 1 to count of dateNames

if item a of dateNames is workingDate then

set value of cell ("E" & fullSheet) to item a of Dailywordcounts

end if

end repeat

else

exit repeat

end if

set fullSheet to fullSheet + 1

end repeat

end tell

save

end tell

end tell

log filenames

log WordCounts

log dateNames

log Dailywordcounts

end update_the_spreadsheet

Let me know if this works for you, or if you have any questions, or suggestions for improvements!

— FGY

Advertisements

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s