Writing Your First PSPad Extension

By Ronald DeJourney, 10/17/2010

PSPad can be extended in 2 ways: JavaScript and VBScript. The following code sample is in javascript.

/*
  Hello World PSPad Extension.
  Author: Ronald Dj
*/

var module_name = "Hello_World";
var version = "1.00";

function helloWorld()
{
  var source = getSource();
  source = source + "Hello World";
  write(source);
}

function getSource()
{
  var ed = newEditor();
  ed.assignActiveEditor();
  var source = ed.selText();
  if (source == '')
  {
    source = ed.Text();
  }
  return source;
}

function write(textValue)
{
  var ed = newEditor();
  ed.assignActiveEditor();
  var txt = ed.selText();
  if (txt == '')
  {
    ed.text(textValue);
  }
  else
  {
    ed.selText(textValue);
  }
}

function Init()
{
  addMenuItem("Write Hello World", "Hello World Plugin", "helloWorld","CTRL+ALT+H");
}
     

Save this at: %PSPad%\Script\JScript\ with a .js file extension, then restart PSPad.

Init()

All PSPad extensions must have the Init()function. It will be called first when PSPad is launched. Its purpose is to update the Scripts menu with an item equalling the module's name, and under that, items equalling each of the module's functions and subs.

  • arg0: The name of your child menu
  • arg1: The name of your parent menu. It will be placed in the Scriptmenu.
  • arg2: The function that will be called each time the extension is executed
  • arg3: The shortcut key to your extension.

Your Function: helloWorld()

This function will be called each time the menu item "Write Hello World" is chosen.

getSource()

getSource() returns the currently selected text. If nothing is selected, it will return the entire document.

  • var ed= newEditor(), this function is built into PSPad API, newEditor() is used to create new object for editor handling.
  • ed.assignActiveEditor(), this will assign the currently active editor window.
  • var source=ed.selText(), this will return the selected text into the variable source.
  • source=ed.Text(), this section will be called if no text has been selected.

write(textValue)

This function places the text into the current editor.

  • ed.text(textValue), this replaces ALLof the text in the editor with textValue.
  • ed.selText(textValue), this will replace only the selected text with textValue.