Trunk Notes scripting

Trunk Notes 2.6.0 is nearly ready for some final testing and release. It is quite a bit speedier than previous versions of Trunk Notes (especially for those with large wikis) and now has it’s own scripting language.

Adding scripting to Trunk Notes makes it even more powerful. Users can now do things in Trunk Notes that could previously only be done in desktop editors. Whilst this is only a starting point there are still lots of things to play with.

The scripting language built into Trunk Notes is Lua, a language ideal for embedding into applications. It is a very small, fast and powerful language which is easy to use and has lots of exciting features. Check out the Lua website for more information.

To whet your appetite here are a couple of examples of Lua scripts in Trunk Notes.

Day/Night example

There are a number of users who have experimented with JavaScript solutions to have Trunk Notes automatically switch between two stylesheets depending on the time of day.

Using the new Lua scripting it becomes possible to do this much more easily – and elegantly.

In my Header page, which gets included in every document, I add a call to a Lua script which will include the relevant stylesheet:

{{lua DayNight.lua}}

This will run the commands contained in the page DayNight.lua and insert the result into the current page.

Here is my DayNight.lua page:

hour = tonumber(os.date('%H')) if hour < 7 or hour > 20 then     return "{{stylesheet Night.css}}" else     return "{{stylesheet Day.css}}" end

As you can see anything returned from Lua is treated as Markdown and included at the point where the script was called.

Manipulating the wiki

What you couldn’t do in JavaScript, even if you tried really really hard, was create new wiki entries. This is now almost trivial. An example:

-- Create a new wiki page (if this page already exists the newpage will be nil) newpage = wiki.new('MyNewPage') -- Put the date in the contents of the page newpage.contents = os.date() -- Tag this as Journal and Lua (tags are created if necessary) newpage.tags = {'Journal', 'Lua'} -- Save the page wiki.save(newpage) -- Return the page name return newpage.title

There are also Lua functions for searching the wiki, getting lists of tags and retrieving existing pages. Arguments can be passed to scripts, e.g. {{lua ExampleScript.lua, argument 1, argument 2}} and retrieved through the args array.

Scratching the surface

I’m looking forward to see what new and innovative uses the Trunk Notes community makes of Lua scripting. Trunk Notes 2.6.0 is going through some final testing now before it is submitted to Apple next week (probably).

Keep having fun with Trunk Notes – stay productive!

(Lua scripting will play a big part in making Trunk Notes for Mac a powerful wiki to have on your desktop. Watch this space!)