5th July, 2020

Making a "Hello World" Javascript Macro in Tiddlywiki

To understand and start using TiddlyWiki in a better manner, lets try to create a simple "Hello World" Javascript Macro.

Start by copying or forking the HelloWorld macro from here. Import this your TW by following the following steps:

  1. Create a new tiddly and paste the code into the edit-text box
  2. Put the title as $:/adithyab/macros/helloworld.js
  3. Enter the type under the textbox as application/javascript
  4. Add a new field with field-name as module-type and field value as macro
  5. Save the tiddly (make sure the wiki is saved too) and refresh your browser tab.

To run the macro, you will need to open a new tiddly with title Hello World Test (or anything else) and paste the following code into the edit-text box: <<helloworld>>. When you save it, you should see the Tiddly show the Hello World! message.

So this HelloWorld macro has an optional parameter you can pass. If you change the call to <<helloworld Adithya>>, you will get a Hello Adithya! message. Now, you can extend this to many more things. I made a small timediff macro which you can find here.

Some tips and tricks

  1. Always make a copy of your TW to try out these things. If things break, you might lose data.
  2. I tested out my function on https://jsbin.com before putting it into the TW
  3. Look at the core JS macros available here: https://github.com/Jermolene/TiddlyWiki5/tree/master/core/modules/macros for reference.
  4. When you make save your macro, it becomes a "System Tiddler" so you can find it by clicking on the more button in the sidebar and looking under the 'System' tab. You cant find it by searching or in the 'recents' tab.
  5. For easy acces to the macro, make a link by putting [[$:/adithyab/macros/helloworld.js]] in one of your starting tiddlies.
  6. The try...catch block makes sure if something breaks in your macro, your TW is still operable. If your get an error, you can get the stacktrace by entering the debugger mode.
  7. Ask the super helpful folk in the TW google group at https://groups.google.com/forum/#!forum/tiddlywiki if you need help with anything.

For a technical description of Javascript Macros, please visit The Tiddlywiki Dev site