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 new_page will be nil)
new_page = wiki.new('MyNewPage')
-- Put the date in the contents of the page
new_page.contents = os.date()
-- Tag this as Journal and Lua (tags are created if necessary)
new_page.tags = {'Journal', 'Lua'}
-- Save the page
wiki.save(new_page)
-- Return the page name
return new_page.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!)
Been playing with this a bit. Took me forever to figure out why my NightTime function wasn’t working, then realized it’s because instead of:
if hour 20 then
I needed:
if hour = 20 then
(I was in the 20th hour)
Can’t wait to work on some other ideas. Hope you post more recipes soon.
I’d really like to know what the source HTML is for the output in Trunk Notes – so I can style/manipulate with CSS/Javascript.
Lua is a dream come true!
In your Journal example above, I’d like to immediately navigate to the new journal page. How can I do this?
I can datestamp the new entry automatically by:
page_name=os.date(‘%Y-%m-%d Journal’)
new_page = wiki.new(page_name)
— already defined? then just open
if not new_page then
new_page = wiki.get(page_name)
end
…but all this does is create the page invisibly in the trunk database.
I’d like to replace the page that contains the {{lua NewJournal.lua}}
Best I can do is return a hyperlink at the end of the lua function above, which gets inserted into :
return ‘Go to [[' .. new_page.title .. ']]’
(of course, I could use this one line alone to insert a link to create a new page, but wouldn’t have full control over tags, etc as the overall function does)
Sorry, above was a little unclear:
I wanted the call to the LUA script to return by navigating to the new page it had created.
Is there a built-in function to do this, e.g. “wiki.load(page_name)”