Writing Plugins in JavaScript and Python


You can write simple plugins for VoodooPad in either JavaScript or Python.  If you're an advanced programmer, you should know that both languages provide a bridge into Apple's Cocoa frameworks.  Python does this through the PyObjC bridge, and JavaScript does this through a bridge using JSTalk.


When writing scripts you should use a plain text editor such as BBEdit, TextMate, SubEthaEdit, Xcode, or many other source editors that are available for the Mac.  If you are writing your scripts in JSTalk, you can also download a simple editor off the JSTalk website.


(If you're new to programming, and you'd like to learn- it will probably be worth your time to visit http://www.codecademy.com for tutorials in writing JavaScript)


Here's a simple example script, which will print out all the pages in a document:


function main(windowController, document) {

    var keys = document.keys();

    for (i = 0; i < keys.length(); i++) {

        print(keys[i]);

    }

}


Copy and paste the script into a new VP page, and choose the Plugin JSTalk Run Page as JSTalk menu item.  You should then see the VP Console pop up with a list of all the pages in your document.


Note: if you get a Syntax Error: it's probably because there is extra text on the page.  Make sure you have only the script as given above on the page.


Next, you can modify the script so that it inserts the names into the current page instead of just printing them out in a new window:


function main(windowController, document) {

    var textView = windowController.textView();

    var keys = document.keys();

    for (i = 0; i < keys.length(); i++) {

        textView.insertText(keys[i]);

        textView.insertText("\n"); // insert a newline

    }

}


If you find this script useful, you can then save it so it shows up in your Plugin menu.  Choose the Plugin JSTalk Save Page as JSTalk Plugin… menu item, give it a good name (and make sure it ends with .jstalk!).  Your script will then show up under the Plugin menu.


If you ever want to delete or modify your script, choose the Help Open VoodooPad's App Support Folder menu item.  You will find a folder named "Script PlugIns" where your script is located.  You can modify, delete, or even add new plugins there.



A Python Example

Python plugins are similar to the JavaScript / JSTalk plugins, but you must wrap them in a main method and give the script a name in the source file like this:


# -*- coding: utf-8 -*-

VPScriptMenuTitle = "List Page Names"

import AppKit


def main(windowController, *args, **kwargs):

    document = windowController.document()

    textView = windowController.textView()

    for key in document.keys():

        textView.insertText_(key)

        textView.insertText_("\n")