Skip to content

Simple "Hello World" extension

peterflynn edited this page Jun 7, 2012 · 26 revisions

Save this file as brackets/src/extensions/user/HelloWorld/main.js to see it running in Brackets.

/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets, window */

/** Simple extension that adds a "File > Hello World" menu item */
define(function (require, exports, module) {
    'use strict';

    var CommandManager = brackets.getModule("command/CommandManager"),
        Menus          = brackets.getModule("command/Menus");

    
    // Function to run when the menu item is clicked
    function handleHelloWorld() {
        window.alert("Hello, world!");
    }
    
    
    // First, register a command - a UI-less object associating an id to a handler
    var MY_COMMAND_ID = "helloworld.sayhello";
    CommandManager.register("Hello World", MY_COMMAND_ID, handleHelloWorld);

    // Then create a menu item bound to the command
    // The label of the menu item is the name we gave the command (see above)
    var menu = Menus.getMenu(Menus.AppMenuBar.FILE_MENU);
    menu.addMenuItem("menu-helloworld-sayhello", MY_COMMAND_ID);
    
    // We could also add a key binding at the same time:
    //menu.addMenuItem("menu-helloworld-sayhello", MY_COMMAND_ID, "Ctrl-Alt-H");
    // Or you can add a key binding without having to create a menu item:
    //KeyBindingManager.addBinding(MY_COMMAND_ID, "Ctrl-Alt-H");
    // (Note: "Ctrl" is automatically mapped to "Cmd" on Mac)

    // For dynamic menu item labels, you can change the command name at any time:
    //CommandManager.get(MY_COMMAND_ID).setName("Goodbye!");    
    
});

Looking for more detailed information? Check out How to write extensions, or look up the above APIs in the Brackets source code for complete docs.

Clone this wiki locally