The Flying Meat Wiki : TriggerExamples

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

Trigger Examples


A trigger 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())
triggerDictionary.document:openPageWithTitle(pageName)


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

-- assign this to the "Page created" event
newPage = triggerDictionary.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 trigger.
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 trigger 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 (triggerDictionary.item:type() ~= VPPageType) then
	return
end
 
-- grab what's going to be sent out to the browser (this is the raw html)
output = triggerDictionary.output
 
-- look for the key $lookingFor$ , and replace it with the string "replaceValue"
-- notice that we assign the value back to the trigger dictionary.  This way, VoodooPad
-- gets the updated value.
triggerDictionary.output = output:replace("$lookingFor$", "replaceValue")


A Trigger 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 = triggerDictionary.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 = triggerDictionary.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 trigger.
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" trigger
-- 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 = triggerDictionary.item
document = triggerDictionary.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


--[[
This trigger code makes templates that will only apply
to links that were first made from a specific page.

This trigger requires the page "NewPageTemplate" with "$fromPage$"
on it somewhere.

It also requires a template page that will be used to import text  
from.  The
one used in this example is called "DailyLogTemplate"

The result is that every new page created as a link from the page  
"Calendar"
will have the contents of "DailyLogTemplate" appended to it.
--]]


-- Assign this page to "Page Created" trigger

-- Just loading some stuff to get us started
newPage = triggerDictionary.item
document = triggerDictionary.document

-- Looking to make sure the page is a page and not something else
if (newPage:type() ~= VPPageType) then
	return
end

--[[
Checks if the text of a new page contains "Calendar" and if so, appends
the text from the "DailyLogTemplate" page to the default text.
--]]
if (string.find(newPage:dataAsAttributedString():string(),  
"Calendar")) then
	otherPage = document:pageForKey("DailyLogTemplate")
	attributedString = newPage:dataAsAttributedString()
	newString = otherPage:dataAsAttributedString():string()
	attributedString:objc_mutableString():appendString(newString)
	newPage:setDataAsAttributedString(attributedString)
end
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by Wikka Wakka Wiki 1.1.6.3
Page was generated in 0.0427 seconds