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'.


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

@srhyse said:

{{on noautolink}}

How did I miss this gem 3yrs ago? Probably it wasn't relevant to me back then but it has become a pain recently
Definitely going to try adding this to my default header to see if I can avoid adding random slashes everywhere


read more