Lua Plugin Snippets
Besides these examples, you'll want to read http://flyingmeat.com/fs/contrib/voodoopad/VPPlugin.h∞ for information on what is available. It's just an Objective-C header file, but it's what we've got to go on for now :)
List all the page names in the current document to the current page.
» install
thelist = ""
textView = windowController:textView()
for key in objc.values(document:keys()) do
page = document:vpDataForKey(key)
thelist = thelist .. page:displayName() .. "\n"
end
textView:insertText(thelist)
Creating a new page named "Lua Rocks"
document = windowController:document()
textView = windowController:textView()
document:createNewVPDataWithKey("Lua Rocks")
textView:insertText("Lua Rocks")
Printing out the contents of the index page
page = document:pageForKey("index")
pageText = page:dataAsAttributedString():string()
vpconsole(pageText)
Lookup the page data for the current page, and change it's contents.
(Note: this is not undoable)
currentPageKey = windowController:key()
pageData = document:pageForKey(currentPageKey)
attributedString = pageData:dataAsAttributedString()
attributedString:objc_mutableString():setString("It's all gone!")
pageData:setDataAsAttributedString(attributedString)
Tell the text view to select all, and then insert the text "Hi".
windowController:textView():selectAll()
windowController:textView():insertText("Hi")
Display a dialog box.
This example displays a dialog box with a default button that says "Go Away"
objc.nsAlert("Hello", "This is a message", "Go Away", nil, nil)
Highlight the selected text yellow.
» install
(this will work best as a plugin, since the Run as Lua Script option deselects the currently selected text before running the script)
textView = windowController:textView()
color = objc.class("NSColor"):yellowColor()
textView:textStorage():addAttribute_value_range_("NSBackgroundColor", color, textView:selectedRange())
Change the font for the current page to Monaco 10
textView = windowController:textView()
Font = objc.class("NSFont")
f = Font:fontWithName_size("Monaco", 10)
Dict = objc.class("NSMutableDictionary")
attrs = Dict:alloc():init():autorelease()
attrs:setObject_forKey(f, "NSFont")
range = objc.luaToNSRange({location=0, length=textView:textStorage():length()})
textView:textStorage():setAttributes_range(attrs, range)
Change the font for the current selection.
textView = windowController:textView()
font = objc.class("NSFont"):fontWithName_size("Helvetica", 14)
textView:textStorage():addAttribute_value_range_("NSFont", font, textView:selectedRange())
List all the pages in the document with a meta value named "todo".
» install
(Metas are a feature only available in VoodooPad Pro)
textView = windowController:textView()
list = "My todo pages list:\n"
for key in objc.values(document:keys()) do
page = document:pageForKey(key)
if (page:metaValueForKey("todo") ~= nil) then
list = list .. page:displayName() .. "\n"
end
end
textView:insertText(list)
Look for the word "@todo" in all the pages, and list page name with the line.
» install
function trim(s)
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end
theList = ""
for key in objc.values(document:keys()) do
page = document:pageForKey(key)
if page:type() == VPPageType then
attString = page:dataAsAttributedString()
for line in objc.values(attString:objc_string():componentsSeparatedByString("\n")) do
if string.find(line, "@todo") ~= nil then
theList = theList .. "\n" .. page:displayName() .. ": " .. trim(line)
end
end
end
end
windowController:textView():insertText(theList)
Reveal document in Finder (uses AppleScript + Lua)
» install
--[[
VPLanguage = lua
VPScriptMenuTitle = Reveal Document in Finder
]]
script = 'set s to "' .. document:fileName() ..'"\n'
script = script .. 'set p to POSIX file s\n'
script = script .. 'tell application "Finder"\n'
script = script .. ' activate\n'
script = script .. ' reveal p\n'
script = script .. 'end tell\n'
os.execute("/usr/bin/osascript -e '" .. script .. "'")
Make a new page with the selected text, without opening it.
» install
--[[
VPLanguage = lua
VPScriptMenuTitle = Make Link Without Opening
VPShortcutKey = l
VPShortcutMask = command shift
VPEndConfig
--]]
textView = windowController:textView()
pageName = textView:objc_string():objc_trim():substringWithRange(textView:selectedRange())
if pageName:len() == 0 then
objc.nsAlert("Sorry, blank words are not allowed")
return
end
document:createNewVPDataWithKey(pageName)
Commit changes to subversion
» install
Subversion is a version control system. If you already have your document in a subversion repository this script will add new files, remove deleted files, and commit the changes. To learn more about subversion, visit http://subversion.org/∞
--[[
VPLanguage = lua
VPScriptMenuTitle = Subversion Commit
VPEndConfig
]]
-- we assume subversion is located in /usr/local/bin/svn
posix.chdir(document:fileName())
-- add new files
os.execute("/usr/local/bin/svn st | " ..
"/usr/bin/grep '^\?' | " ..
"/usr/bin/sed -e 's/\?[ ]*//g' | " ..
"/usr/bin/xargs /usr/local/bin/svn add")
-- clean up deleted pages
os.execute("/usr/local/bin/svn st | " ..
"/usr/bin/grep '^\!' | " ..
"/usr/bin/sed -e 's/\![ ]*//g' | " ..
"/usr/bin/xargs /usr/local/bin/svn rm")
os.execute("/usr/local/bin/svn ci -m'auto commit'")
os.execute("/usr/local/bin/svn st")
vpconsole("Commit complete.")