Home > Extensions > Writing PSPad Hello World Extension

Writing PSPad Hello World Extension

All of this blog readers, must have his/her most fave text editor…. In my case, i have  PSPad as my text editor, it is a freeware and has so many beautiful features inside for software developer, such as: syntax highlighting according to file type, Hex Editor, templates, and still many more…

Notably, as this post’s title said, i will share about one of PSPad marvelous feature, Extension or in the other name we can call it Plug-in. Well basically, PSPad feature can be extensible with 2 ways of scripting: JavaScript and VBScript. I will only show how it works with Javascript, cause honestly i know nothing about VBScript, and have no interest at all in doing something with that 🙂

Here are some numerous things that you have to prepare to build your own PSPad Extension:

  • Your text editor,  of course you can use PSPad in here.
  • Knowledge about JavaScript.
  • Good internet connection, just in case you have to browse some codes to help you build the extension.

If these things already checked, we can start to code… And here is my scenario: i want to build the extension that can automatically print Hello World to the active window. As our ancestors said, Hello World is a must for newbie :D. Pretty simple, and easy enough to code it in JavaScript.

Here is the full code

/*
Hello World PSPad Extension.
Author: Ronald Dj
https://namingexception.wordpress.com
*/

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);
 }

}

i got it too fast? don’t worry i will explain the code one by one, but just in case you are the code-haters, just copy-paste the code and save it with  .js filetype and save it into%PSPad Installation Folder%\Script\JScript\ , restart your PSPad, and then go to toolbar  Scripts -> Hello World Plugin -> Hello World,  see what is happening in here 😀

All PSPad extensions, will started from one function, the Init() function. It will be called first when PSPad being launched. Usually we use this function to place our extension’s menu.  As it coded below, we use addMenuItem(arg0, arg1, arg2, arg3); to do it.

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

Here is the addMenuItem explanation:

  • arg0: “Write Hello World”, this will be our child menu.
  • arg1:  “Hello World Plugin”, this will be the parent menu and will be placed in Script menu.
  • arg2: “helloWorld”, this is the function that will be called each time it’s pressed, look the full source carefully and you can see the helloWorld().
  • arg3:  “CTRL+ALT+H”, as you predict, this is your shortcut key to your extension.


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

This helloWorld() will be called each time the menu “Write Hello World” pressed, it took only 3 lines, simple huh and nothing to really explain in here, the more complicated located in getSource() and write(text).

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

 return source;
}

getSource() has its functionality to return current selected text, if there is nothing selected, it will be return the whole text, i will be split it into 5 sections, hopefully that will be help you to understand more about it.

  • var ed= newEditor(), this function is built in PSPad API, newEditor() is used to create new object for editor handling.
  • ed.assignActiveEditor(), this syntax will assign currently active editor window.
  • var source=ed.selText(), this function will return selection  text into variable source.
  • source=ed.Text(), this section will be called, if your mouse select nothing and ed.Text() will return all of the editor text into variable source, don’t care about if it selected or not, just return ALL of the text.
  • return source, this will return the text, and it’s the end of getSource().
function write(textValue)
{
 var ed=newEditor();
 ed.assignActiveEditor();
 var txt = ed.selText();
 if(txt=='')
 {
 ed.text(textValue);
 }

 else
 {
 ed.selText(textValue);
 }

}

write(textValue) as its named, this function will write the text into current editor. I will split it into  3 sections.

  • var ed= newEditor(), ed.assignActiveEditor(), and var txt =ed.selText() have the same explanations with the getSource() section.
  • ed.text(textValue), this will replace ALL of  the text in the editor with the textValue.
  • ed.selText(textValue), this will replace only the selected text with the textValue.

Well, i’m done with my explanation. Now it’s your turn to learn, extend, and use it in the real world. I’ll post about another PSPad Extension next time, and enough for now. Oh yeah, for addition you can visit, http://gogogadgetscott.info/pspad/functions.html, about PSPad API completely.

Advertisements
Categories: Extensions
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: