Lesson 6: Removing Books
| filename: | lesson06-remove_book.py |
| getName() | Lesson 06 - Remove Book |
| getDescription() | Lesson 06 - Learn how to remove a book from your library |
Here is a shorter lesson just to finish up with books for now - we're going to see how to remove a book.
Setting up - Create a Book
Rather than remove one of your precious books, we're going to create our own expendable book. Lifting the book creation code
straight from an earlier lesson, we create a book called "Lesson 6 Removable Book":
def lesson06_setupBook( outputFile, newBookName ): collection = theAppData.library.getDataSource( 0 ) book = WikiBook( collection ) book.setDisplayName( newBookName ) ret = theAppData.library.addBook( book, collection ) if ret: outputFile.write( "adding book '%s' was successful\n"%( newBookName ) ) else: outputFile.write( "adding book '%s' was NOT successful\n"%( newBookName ) ) homePageName = "My Home Page" homePageText = "This is my home page. It worked!" book.addPageWithNameAndContents( homePageName, homePageText ) book.setHomePage(homePageName)
Removing a Book
This unfortunate short-lived book is about to be immediately removed. First we create a book object:
book = theAppData.library.findBook( newBookName )
Then we pass that as a parameter to the 'removeBook' function:
theAppData.library.removeBook( book )
When you run the plugin, it runs too fast to see the book. The only evidence that it actually worked is in the log file 'lesson06.txt'. But you can modify the plugin so that is removes one of your books, if you want to see it work with your own eyes.
You may have noticed a difference between removing a book via a plugin, and removing a book manually within Note Studio. Note
Studio the application has extra functionality to make Note Studio easier and safer to use. The Plugin API has no such niceties.
So, while Note Studio will ask you 'Are you sure?', to verify that you actually indended to remove a book, the plugin API
will just do it, with no questions, and no compunction.
Conclusion:
Now that you've seen how to create, import, export and remove books, you have the ability to control the contents of the books
in your library. Next, it's time to turn our attention to individual pages and their contents.
API Functions:
WikiLibrary.removeBook( book )