Flying Meat
VoodooPad Docs: Events

Events

toolbar32-eventscripts.png

This is a VoodooPad only feature.  Events 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.


Events 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 events is to create one. Start off by creating a new page named "New Page Event", and delete all the text on the new page so you have a clean slate.  Next, copy and paste this quick script:


newPage = eventDictionary.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, assign this script to an event in VoodooPad. Bring up the Event Scripts Palette (Window Palettes ▸ Event Scripts).  Select the event "Page created" for the "On event:" label, and then press the plus button at the bottom of the window to select the new page:



Pasted Graphic.tiff



Press the "OK" button and you've created your first event. You should then see something like this in the Event Scripts window:



Pasted Graphic 1.tiff



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.  Below is a look at this script with a little more detail.


newPage = eventDictionary.item


Every event that is executed has a couple of global variables setup automatically.  One of these is named "eventDictionary".  This dictionary contains values that can be of use when your events 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(eventDictionary, vpconsole)


In the first line of the example, you are assigning the value "item" of the event 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 you get a single line of text that is sent to the console.  The newPage:displayName() bit calls a method named "displayName" on the new page object.  A couple of other methods that can be called are "modifiedDate", "createdDate", "key", "type".  There are more, so explore! 


Next, take the contents of your new page and modify them a little bit by adding the current date to it. There are a couple of easier ways to do this, but for this example pretend there are not any.  To start, go back to your "New Page Event" page and delete all the text there, and replace it with this:


newPage = eventDictionary.item

attributedString = newPage:dataAsAttributedString()

newString = os.date("\nThis page was created on %A, in %B")

attributedString:objc_mutableString():appendString(newString)

newPage:setDataAsAttributedString(attributedString)



What to do here:


1: Grab the new page that is 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 your date string that will append to the end of your new page, in the format of "This page was created on Tuesday, in May".

4: Append your new text to your attributed string.

5: Set that attributed string back into the page so it will show up when you view it.


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


Other Topics:



Back to VoodooPadVoodooPadScripting