Lesson 2: Finding Your Books
Last lesson, we inserted some lines which would open and write a file. The few lines we added really had nothing to do with
Note Studio, they were just simple Python instructions.
If you just shook your head and said 'wha...?', it's time for some quick background. Skip over this section if you know about
Python programming.
The Python Programming Language
Python is the programming language used to write Note Studio plugins. It happens to be a simple, powerful language which is
relatively easy to learn. If you have a basic (and it can be very basic) knowledge or interest in programming, but know nothing
about Python, you can quickly pick up what you need.
Rather than give any tutorial ourselves, we'll just point you at the official Python tutorial - it explains Python far better
than we could. The tutorial is quite brief, but contains everything about Python you will need to continue these lessons.
Some links, then:
| Official Python Website | http://www.python.org |
| Python Online Tutorial | http://docs.python.org/tut/tut.html |
Looking at Note Studio Data
OK, welcome back. Once you're here, we assume you have a basic grasp of the Python language. Now it's time to see how to access
Note Studio data. First, make another copy of _template.py, and set it up as follows:
| filename: | lesson02-list_books.py |
| getName() | Lesson 02 - Books |
| getDescription() | Lesson 02 - Browse Note Studio Data and List All Books. |
The first thing we need to do is add an extra include at the top of the file:
from AppData import *
Data in Note Studio is accessible via a global object called 'theAppData'. This is included in the AppData module, which we just imported with the above line. Let's skip down to the execute() function, and see how to read data from
this object.
For a start, here's how you can query how many books you have:
numBooks = theAppData.library.getNumBooks()
What's going on here? Well, 'theAppData' is the basic object which you use to access Note Studio data. The most important object in theAppData is called 'library'. What type is this object? Well, it's a class called 'WikiLibrary'. At the end of these tutorials, you will find an API reference. WikiLibrary has a method called 'getNumBooks' which returns (surprise surprise) the number of books in the library. This is the total count of books in all your collections.
Knowing that number, we can now loop through the books, and get each name:
book = theAppData.library.getBook( i )
Here, book is a 'WikiBook' object. Once you have that, you can call any of the WikiBook methods on it.
The entire code we inserted into the execute() function, then (including some code to write the results to our output file), is
outputFile = open( "C:/lesson02.txt", "w" ) outputFile.write( "Lesson 02\n\n" ) numBooks = theAppData.library.getNumBooks() outputFile.write( "Number of Books: %d\n\n"%numBooks ) outputFile.write( "#, Book Name:\n" ) for i in range( numBooks ): book = theAppData.library.getBook( i ) bookName = book.getDisplayName() outputFile.write( "%d, '%s'\n"%(i, bookName) ) outputFile.close()
Conclusion
Now you know how to look through your library, and find each book. This means you could find a specific book, if you knew
its name. Next, we'll see how to access the pages themselves, inside a book!