The Flying Meat Wiki : VoodooPadEventScripts

HomePage :: Categories :: PageIndex :: RecentChanges :: Login/Register

Event Scripts (VoodooPad Pro Only)


A VoodooPad Pro only feature - Event Scripts 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: Event Scripts are written in either the scripting language Lua or Python, and a little bit of programming experience is required. You can find out more about Lua from http://www.lua.org/ and more about Python from http://www.python.org/ .

The easiest way to learn Event Scripts is to create one. Start off by creating a new page named New Page Script, 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 Event Scripts palette either by selecting it in an open palette, or by choosing Window ▸ Palettes ▸ Event Scripts. Select On event ▸ Page created and then press the plus button at the bottom of the window to select our new page. Make sure to also choose Language ▸ Lua since this example is written in Lua.

image

Press the "OK" button and we've created our first Event Script. You should then see something like this in the Event Scripts palette:

image

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 Event Script 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 Event Scripts 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 Event Script 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 Script" 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.

If you're curious about Python scripting, it works pretty much the same way, but with Python. Check out the Event Script Examples for more information.

Also check out:
Event Script Examples
Lua website
Python website

Back to VoodooPadPalettes
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by Wikka Wakka Wiki 1.1.6.3
Page was generated in 0.0381 seconds