The Code
For the purposes of this hack, I am keeping the
code for HAWG.widget as bare-bones as possible,
forgoing an About box and other optional bits, which you can add if
you prefer. It won't be particularly pretty, but
it'll work:
<?xml version="1.0" encoding="UTF-8"?>
<widget version="1.0" debug="on">
<!--
HAWG 1.0
by Me
Based on Cap'n Hector's Zen Monitor
It's not pretty, but it works! ;-)
-->
<window title="HAWG" name="main_window" width="500" height="500" alignment="left"
visible="true" />
<textarea>
<name>HAWGS</name>
<size>12</size>
<columns>100</columns>
<lines>25</lines>
<bgColor>#FFFFFF</bgColor>
<bgOpacity>250</bgOpacity>
<editable>false</editable>
</textarea>
<action trigger="onLoad">
function updateHAWGS( ) {
HAWGS.data = "HAWGS: " + runCommand("top -l 2 -ocpu -F -n3")
}
updateHAWGS( );
main_window.visible = true;
</action>
<action trigger="onTimer" interval="10">
updateHAWGS( );
</action>
<action trigger="OnMouseDown">
closeWidget( );
</action>
</widget>
The first part of the code is simply the XML declaration, followed by
a bit of metadata about the program. The real programming begins with
the window declaration that defines how large our
widget's main window is going to be. Currently,
HAWG is a screen hog and takes up a large chunk
of space. This 500 by 500
window spread is invisible, however, until we stick something into
it. I coded all the windows definitions in one line, but notice that
for defining the text area, I switched to the other style of XML
input that Konfabulator understands. I created a
textarea named HAWGS (this is
the handle by which I can refer to this textarea
later in the program), defined its size so that it will actually hold
all of the top output without scrolling, and made
it white (in order for bgColor to work in
textareas, you must also define opacity using
bgOpacity) and noneditable. I did not include any
data, so without anything else, this will simply display a white
rectangle on the screen. We're relying on
top to provide the text that will be displayed in
the box, and for that we need to set up an action and define a
function for that action.
I've set up three different actions. The first,
onLoad, launches (as the name suggests) when you
launch HAWG.widget in Konfabulator.
I've defined a function called updateHAWGS(
), which drops a HAWGStextarea into place with "HAWGS:
", followed by the results of our
runCommand. Right after defining this function, we
invoke it, make sure the window is visible, and come to the end of
the onLoad action.
The second action simply automates the updateHAWGS(
) function on a regular interval (every 15 seconds) using
the built-in onTimer trigger.
Because there is no static text field defined in our widget, it is
nearly impossible to Control-click on the HAWG.widget
window (there's nothing to grab hold of)
to navigate to Konfabulator's contextual menu. This
makes the widget hard to close, so I've included a
third action that invokes closeWidget() when you
click (OnMouseDown) anywhere on the
HAWG.widget window.