Flying Meat
VoodooPad Docs: Event Examples

Event Examples


A event that creates a new page based on the current date when a document opens


-- assign this to the "Document did open" event.

pageName = os.date("%Y.%m.%d", os.time())

eventDictionary.document:openPageWithTitle(pageName)



A simple event that you could use to modify the default text on a new page


-- assign this to the "Page created" event

newPage = eventDictionary.item

-- check and see if we are dealing with a regular page type.

-- if it's not a regular page type, then bail out on this event.

if (newPage:type() ~= VPPageType) then

return

end

 

-- we grab the contents of the page

attributedString = newPage:dataAsAttributedString()

 

-- using objc_mutableString() insteadof mutableString() will return an objective-c object which we can then

-- call a function on, instead of a lua string.

attributedString:objc_mutableString():appendString("\nThis is an addition to the new page text.")

attributedString:objc_mutableString():appendString("\nThis new page's name is '" .. newPage:displayName() .. "'")

 

-- and now we set the value in the new page.

newPage:setDataAsAttributedString(attributedString)



A WebServer event example


-- assign this to the "Web server will send page" event

 

-- check and see if we are dealing with a regular page type.

-- if it's not a regular page type, then return.

if (eventDictionary.item:type() ~= VPPageType) then

return

end

 

-- grab what's going to be sent out to the browser (this is the raw html)

output = eventDictionary.output

 

-- look for the key $lookingFor$ , and replace it with the string "replaceValue"

-- notice that we assign the value back to the event dictionary.  This way, VoodooPad

-- gets the updated value.

eventDictionary.output = output:replace("$lookingFor$", "replaceValue")



An Event to update a page listing all the pages in the document


-- assign this to the "Page Deleted" and "Page Created" events.

-- the index is created in a page named "Page Index"

indexPageName = "Page Index"

document = eventDictionary.document

list = "All the pages in this document:\n"

for key in objc.values(document:keys()) do

page = document:pageForKey(key)

list = list .. page:displayName() .. "\n"

end

document:createNewPageWithName(indexPageName) -- noop if it's already around.

pageData = document:pageForKey(indexPageName)

attributedString = pageData:dataAsAttributedString()

attributedString:objc_mutableString():setString(list)

pageData:setDataAsAttributedString(attributedString)



Replace the template placeholder $clipboard$ with the text on the clipboard


-- assign this to the "Page created" event

newPage = eventDictionary.item

-- check and see if we are dealing with a regular page type.

-- if it's not a regular page type, then bail out on this event.

if (newPage:type() ~= VPPageType) then

return

end

-- we grab the contents of the page

attributedString = newPage:dataAsAttributedString()

-- grab the clipboard

p = objc.class("NSPasteboard"):generalPasteboard()

t = p:stringForType("NSStringPboardType")

range = objc.luaToNSRange({location=0, length=attributedString:objc_mutableString():length()})

if (t ~= nil) then

count = attributedString:objc_mutableString():replaceOccurrencesOfString_withString_options_range_("$clipboard$", t, 0, range)

else

-- otherwise just make the $clipboard$ placeholder an empty string

attributedString:objc_mutableString():replaceOccurrencesOfString_withString_options_range_("$clipboard$", "", 0, range)

end

-- and now we set the value in the new page.

newPage:setDataAsAttributedString(attributedString)



Paste text from a template page based on the title of the newly created page


-- Assign this to the "Page Created" event

-- This script requires your document to have a page called PlasmidTemplate.  If you have another page as your template, change the appropriate variable.

-- Page variables such as $date$ won't work with this script


newPage = eventDictionary.item

document = eventDictionary.document


-- Making sure the page we're creating is a text page


if (newPage:type() ~= VPPageType) then

return

end


-- Checking to see if the page title includes the word "plasmid" and if so, pulling the content from the "PlasmidTemplate" and pasting it onto the new page, replacing the content.  ie, if you created a new page called "plasmid power" it would pass.


if (string.find(newPage:displayName(), "plasmid")) then

pageTemplate = document:pageForKey("PlasmidTemplate")

pageText = pageTemplate:dataAsAttributedString()

newPage:setDataAsAttributedString(pageText)

end


Back to VoodooPadVoodooPadScriptingEvents