JSCocoaCodaLoader is a silly name, isn't it?

Coda 1.6 was just released, and it's got support for 3rd party plug-ins now. Woo! And of course I had to go and write a loader for Lua like I did for VoodooPad. Wait no- not Lua, I mean Python. Wait, um... no, not Python this time either. Ok, I picked Javascript, using the cocoa-javascript bridge from JSCocoa.

What this plugin does, is enables you to write plugins that work in Coda, but in Javascript.

Here's an example:
// Uppercase.js
codaPluginsController = JSCocoaLoaderPlugIn.codaPluginsController();
var tv = codaPluginsController.focusedTextView(null);
var selectedText = tv.selectedText();

if (selectedText != null) {
    tv.insertText_(selectedText.uppercaseString())
}


Put that in a file named "Uppercase.js", inside ~/Library/Application Support/Coda/JSPlug-ins/ (which is different than ~/Library/Application Support/Coda/Plug-ins/, which is where the normal Coda plugins go), and it'll show up under the Plug-ins menu.

Here's another one, for pasting an href around some selected text:

//PasteURL.js
codaPluginsController = JSCocoaLoaderPlugIn.codaPluginsController();
var tv = codaPluginsController.focusedTextView(null);
var selectedText = tv.selectedText();

if (selectedText != null) {
    // for simplicity, no real checking is done here
    var clipboard = NSPasteboard.generalPasteboard().stringForType(NSStringPboardType);
    tv.insertText_("<a href=\"" + clipboard + "\">" + selectedText + "</a>")
}


You can download the plugin: JSCocoaLoader.codaplugin.zip, or check out the source from my svn repository. Unzip and put the JSCocoaLoader.codaplugin in ~/Library/Application Support/Coda/Plug-ins/. If it's not there already, create it.

Tada!