Sample Document Events

Here are some example document events that you can use in your own documents or modify to suit your needs:

Creating and opening up a new page based on the date every time you open a document

function documentWasOpened(document) {
    var now = new Date();

    var year = now.getFullYear();
    var month = now.getMonth() + 1; //months are zero based
    if (month < 10) {
        month = "0" + month;

    var day = now.getDate();
    if (day < 10) {
        day = "0" + day;

    var newPageName = year + "." + month + "." + day


Using GIT to add and commit changes to your document on open and close

Git is a version control system that you can install on your Mac. You can then use the following document event script to commit new and altered pages to a Git repository, located in your document. If a Git repository does not already exist, the script will create one for you.

Note: This script assumes Git is installed in /usr/bin/git. If you have it located somewhere else, you will need to alter the script.
function runGitWithArgsForDocument(document, args) {
    task = NSTask.alloc().init().autorelease();

function documentWasOpened(document) {
    var docURL = document.fileURL();
    var gitURL = docURL.URLByAppendingPathComponent_('.git');
    var fm = NSFileManager.defaultManager();

    if (!fm.fileExistsAtPath_(gitURL.path())) {
        print("Making the git repository");

        runGitWithArgsForDocument(document, ['init']);
        runGitWithArgsForDocument(document, ['add', 'pages', 'properties.plist', 'storeinfo.plist']);
        runGitWithArgsForDocument(document, ['commit', '-m', 'First Commit']);

function documentWillClose(document) {
    // adding pages again will just add any new pages that didn't already exist.
    runGitWithArgsForDocument(document, ['add', 'pages']);
    runGitWithArgsForDocument(document, ['commit', '-a', '-m', 'document close']);

function documentWasClosed(documentPath) {