The Basics

Editing

Web Publishing

Scripting

Dynamic Content

Events

Palettes

Managing Links, Pages, and Importing

VoodooPad on your iOS Devices

Security and Privacy

Miscellaneous

Page Events

Page events are a way to run JavaScript code when pages are opened, closed, created, or deleted.

To get started with page events, you'll first want to create a page in your document named "VPPageEventScript". This will automatically create a page with some required functions as shown below.

The default page that opens up will just print out the events for you, but you can modify the functions to perform certain tasks, like appending the current date to a page:

function pageWasCreated(document, item) {
    var attString = item.dataAsAttributedString();
    attString.mutableString().appendString("\nThis page was created on " + (new Date()));
    item.setDataWithAttributedString(attString);
}

Here is the default VPPageEventScript page:

/*
    The pageWasOpened method is called when a page is opened in a VoodooPad document for viewing.
 */

function pageWasOpened(document, item) {
    print("pageWasOpened: " + item.displayName());
}


/*
    The pageWasClosed method is called after a page is closed in a VoodooPad document.
    Note: if a page is deleted, this method will not be called.
 */

function pageWasClosed(document, item) {
    print("pageWasClosed: " + item.displayName());
}


/*
    The pageWasCreated method is called after a page is created in a VoodooPad document.
 */

function pageWasCreated(document, item) {
    print("pageWasCreated: " + item.displayName());

}


/*
    The pageWasDeleted method is called after a page is deleted in a VoodooPad document.
 */

function pageWasDeleted(document, pageName) {
    print("pageWasDeleted pageName: " + pageName);
}

/*
 The textViewWillDisplay function is called right before a text item's NSTextView is about to display.
 You can take this opportunity to customize it with things like specific margins:
 */

function textViewWillDisplay(document, page, textView) {
    // uncomment to add a custom 30 pt margin in the sides of the text view.
    // [textView setTextContainerInset:NSMakeSize(30, 0)];
}

A Page Event Example to Find Orphaned Pages

Here's an example which you can put in your VPPageEventScript, which will watch for pages being opened, and if the name is "Orphaned Pages", then it will look through the document for any pages that aren't linked to.

function makeOrphanedPage(document) {

    var linkedPages = {}; // using a hash table here because it's convenient.
    var orphanedPages = [];
    var summaryPageName = "Orphaned Pages";

    /* Collect all linked pages. */
    var keys = document.keys();
    for (i = 0; i < keys.length(); i++) {
        var page = document.pageForKey(keys[i]);

        if (!page.isText() || (page.key() == "orphaned pages")) {
            continue;
        }

        var pageContent = page.dataAsAttributedString();
        var linkedPageNames = document.linkedPageNamesInAttributedString(pageContent);

        for (j = 0; j < linkedPageNames.length(); j++) {

            linkedKey = linkedPageNames[j].vpkey();


            // ignore self links.
            if (!linkedKey.isEqualToString(page.key())) {
                linkedPages[linkedKey] = "";
            }
        }
    }

    /* Now compare the linked pages with all pages to find the orphaned ones. */
    for (i = 0; i < keys.length(); i++) {
        var key = keys[i];
        if (linkedPages[key] == null) {
            orphanedPages.push(key);
        }
    }

    /* Create a summary page and open it. */
    var summaryPage = document.createNewPageWithName(summaryPageName);
    summaryPage.setDataAsString(summaryPageName + "\n\n" + orphanedPages.join("\n"));
    document.openPageWithTitle(summaryPageName);
}

function pageWasOpened(document, page) {
    if (page.key() == "orphaned pages") {
        makeOrphanedPage(document);
    }
}