In the course of modifying the desktop command so that I could invoke it from within my Java application I found a few bugs:
1) a place where a null result wasn't tested for before use (in taskBarButtonListener method).
2) incorrect handling of popup menu activation detection (mousePressed method...note that mouseReleased was added and also an isPopupTrigger test method called maybeShowPopup).
In addition changes were made to allow the desktop to be invoked and dismissed and reinvoked multiple times.
I've attached the modified command script (called pedesktop.bsh). The places I changed (both for application invoking and for error corrections are marked with a //** comments.
Hope this is of some help to you.