Flying Meat
FS • VoodooPad: Triggers
Make sure to check out the new wiki located at http://flyingmeat.com/wikka/. This one will eventually be retired.

Triggers

A VoodooPad Pro only feature- triggers are a handy way to script VoodooPad when certain events occur, such as a new page being created or deleted, documents opening or closing, files being written on web export and more.

Note: triggers are written in a scripting language named Lua, and a little bit of programming experience is required. You can find out more about Lua from http://www.lua.org/ .

The easiest way to learn triggers is to create one. Start off by creating a new page named "New Page Trigger", and delete all the text on the new page so we have a clean slate to work with. Next, just copy and paste this quick script:

newPage = triggerDictionary.item
vpconsole("we're creating a new page named " .. newPage:displayName())

What this script does is print out the name of the new page in the VoodooPad console. Next we need to assign this script to an event in VoodooPad. So bring up the Page and Document Inspector (under the "View" menu) and switch to the Triggers tab. Select the event "Page opened" for the "On event:" label, and then press the plus button at the bottom of the window to select our new page:

Press the "OK" button and we've created our first trigger. You should then see something like this in the inspector window:

Now try creating a new page in VoodooPad named "random". You should see the text "we're creating a new page named random
" pop open in a little window. Go ahead and delete the random page, and let's take a look at this script with a little more detail.

newPage = triggerDictionary.item

Every trigger that is executed has a couple of global variables setup automatically. One of these is named "triggerDictionary". This dictionary contains values that can be of use when your triggers are being run. An easy way to find out what all the values in the dictionary are is to use this simple line that will print out the keys in the dictionary:

table.foreach(triggerDictionary, vpconsole)

So in the first line of our example, we are assigning the value "item" of the trigger dictionary to a new variable named "newPage". This represents the page that has just been created.

The next line simply prints out some text with the name of the page to the console:

vpconsole("we're creating a new page named " .. newPage:displayName())

The two dots (..) is how Lua concatenates strings together, so we get a single line of text that we send to the console. the newPage:displayName() bit calls a method named "displayName" on the new page object. A couple of other methods that we could call are "modifiedDate", "createdDate", "key", "type". There are more, but we won't get into those right now. Instead, let's spice up our example a little more.

Next, we're going to take the contents of our new page and modify them a little bit by adding the current date to it. There are a couple of easier ways we could do this, but for this example let's just pretend there aren't. So to start, let's go back to our "New Page Trigger" page and delete all the text there, and replace it with this:

newPage = triggerDictionary.item
attributedString = newPage:dataAsAttributedString()
newString = os.date("\nThis page was created on %A, in %B")
attributedString:objc_mutableString():appendString(newString)
newPage:setDataAsAttributedString(attributedString)

What we do here is:

1: Grab the new page that's being created.
2: Grab the attributed string out of the page. An attributed string is a technical way of saying "stylized text". It is what keeps track of your bold and italic text, custom margins, and other fancy text stuff.
3: Create our date string that will will append to the end of our new page, in the format of "This page was created on Tuesday, in May"
4: Append our new text to our attributed string
5: Set that attributed string back into the page so it'll show up when we view it.

Now whenever we create a new page, you'll see that text appended to the end of it.

Other Topics: