Discuss all things Trunk Notes
lua count function

I always wanted to have an easy way to count the number of items returned by a Trunk Notes list function, especially {{tagged}} and {{action}}. It turned out to be easier to accomplish in lua than I expected. Note that the script below uses the wiki.exprl function, with an 'L', not a '1'.

Count.lua:

fn_name = args[1]
fn_args = args[2]

my_list = wiki.exprl(fn_name, fn_args)

count = #my_list
return count

So, to find out how many pages are tagged with ".Project", I use:

{{lua Count.lua, tagged, .Project}}

Script returns: 82 (Yikes)

One limitation: the tagged function accepts more than one tag and will return a list of those pages with tag1 AND tag2 AND ... tagn. This script only works with one tag.
With some finagling, I can make it work with up to two tags:

fn_name = args[1]
fn_arg1 = args[2]
fn_arg2 = args[3]

my_list = wiki.exprl(fn_name, fn_arg1, fn_arg2)

count = #my_list
return count

This works properly with the above invocation and with the following:

{{lua Count.lua, tagged, .Project, .ThisWeek}}

and returns 14. (That's a little better.) I don't think I'll need more than two
arguments here, but you can see how to extend it if needed.

read more
Get help from the community
Date order sort with {{action}}

@srhyse Thanks, that helps me with the syntax for my other question, but here I want to sort by the date I've spelled out in the entry, which I thought was the default behavior for action, but it doesn't seem to be happening properly. Oddly, if I put a time in the entry, even though I'm using a date-only date format, it seems to throw the date sort off, depending on the time of day when the function is executed.

read more