Listing word counts for drafts with specified tag

I’d like to create a writing log which lists the word count for each draft with a specified tag, such as “blog”. The purpose is to discover how much I am writing daily/weekly.

I started with the script below by searching inbox drafts to find the ones with the tag.

My first question is how do I access the data in the returned object? I need to get the title, creation date, and word count.

The following code is not showing what I need in the log.

let myStringArray = Draft.query("", "inbox", ["blog"]);

let arrayLength = myStringArray.length;

 for (var i = 0; i < arrayLength; i++) {

The query function returns a list of drafts. When you loop through that list, you can get the properties of each draft in turn. See the Instance Properties section of the Draft library documentation. The word count can be done in different ways; the easiest is to split on whitespace.

Here’s an example that assumes you’re starting with a blank draft and using it to summarize the blog posts in your inbox.

var blogDrafts = Draft.query("", "inbox", ["blog"]);
var wct = 0;
for (var i=0; i<blogDrafts.length; i++) {
  var t = blogDrafts[i].title;
  var d = blogDrafts[i].createdAt;
  var wc = blogDrafts[i].content.trim().split(/\s+/).length;
  draft.content += t + "\n" + d + "\n" + wc + " words" + "\n\n";
  wct += wc;
draft.content += "Total: " + wct + " words";

Thanks so much @drdrang. This is very cool.

@agiletortoise, this type of action could be a good selling point to people doing writing challenges like NaNoWriMo. Quite often people post the number of words they have written each day on social media. Might be a possible marketing opportunity for Drafts.