Lesson 1: 'Hello World'
The traditional introduction to any programming topic is to write 'Hello World'. We're not going to write 'Hello World' to
the screen yet (that will come later). We're going to write it to a file.
Now, we could start by giving you all the background information you will eventually need. Boring - let's jump straight in
and do something. We can fill in the gaps later.
Starting a New Plugin
Plugins are found in the {Note Studio Install Directory}/plugins. When you put a plugin in this directory, it will show up
on the 'Tools' menu. In the plugins directory, there lives a plugin called called '_template.py'. This is the bare-minimum
plugin. It does, literally, nothing. It shows up on your 'Tools' menu as "Do Nothing", which is incredibly accurate, because
when you run it, it does nothing.
Why does it exist? Well, it is a basis for us to write useful plugins. When you want to write a completely new plugin, you
can copy this plugin, and start adding to it.
Make a copy of '_template.py', and call the new file 'lesson01-hello_world.py'. Open the new file in a text editor, and look
to where you see:
def getName(): # # This is the name that appears on the 'plugin' menu. # It should be unique # return "Do Nothing"
Change it to:
def getName(): # # This is the name that appears on the 'plugin' menu. # It should be unique # return "Lesson 01: Hello World"
It is a good habit to also update the plugin's description. Try changing the getDescription() function from:
def getDescription(): # # A brief, helpful description of the plugin # return "An empty plugin which does nothing. Can be copied as a template."
To:
def getDescription(): # # A brief, helpful description of the plugin # return "Lesson 01 - Create a file containing the text 'Hello World!'."
Make sure you save the file, then run Note Studio. In your 'Tools/Plugins' menu, you will now see a plugin called 'Hello World'.
When you hold your mouse over the the menu item, the status bar will show your description text. For now, the plugin doesn't
do anything when you run it. But that's OK. You've started your first plugin.
Writing a File
In the first few lessons, we're going to write information to files. Later on, we can do more advanced stuff, like popping
up dialogs, and reporting information to the screen. But for now, we'll write boring old text files. We can check our plugin
worked by reading the text files.
Let's go to the real part of the plugin which actually does the work. Currently it looks like this:
def execute( parameterList ): # # Here is where you actually do the plugin tasks. # # If anything goes wrong, you can throw a WikiPluginException # as follows: # STAGE_NAME = "execute" bThrowException = False if bThrowException: raise WikiPluginException( STAGE_NAME, "Some error string describing the problem" )
After the line which says:
bThrowException = False
add the following:
outputFile = open( "C:/hello.txt", "w" ) outputFile.write( "Hello World!" ) outputFile.close()
Now, there's one important thing you need to check: where we are writing the file. I've assumed you are running Windows, and are able to write a file in C:\.
If you run Windows, you may have a preferred location to write a text file. If so, use that instead. If you use an alternate,
unix-based operating system (Linux, Mac OSX, etc), you might want to write it somewhere like "~/hello.txt". You should make
sure you write the file to a good location.
The entire function should now look as follows:
def execute( parameterList ): # # Here is where you actually do the plugin tasks. # # If anything goes wrong, you can throw a WikiPluginException # as follows: # STAGE_NAME = "execute" bThrowException = False outputFile = open( "C:/hello.txt", "w" ) outputFile.write( "Hello World!" ) outputFile.close() if bThrowException: raise WikiPluginException( STAGE_NAME, "Some error string describing the problem" )
Run Note Studio and run this plugin. This should result in a new file being created. Using your shell, locate that file and
open it. You should see the text: "Hello World!".
Congratulations - you've written your first plugin.