Pashua: Creating GUIs for command-line PHP scripts

[share]

Pashua is ”… a tool for creating native Aqua dialog windows for Perl, PHP, Tcl, Python, Rexx and shell scripts as well as AppleScript …” written by Carsten Blüm. The GUI definition is quite simple and straightforward, basically you write a configuration files that has a syntax reminiscing of an .ini file, and Pashua will use it to draw the dialog elements in the order in which they appear in the configuration. In the configuration file you define variables, assign their types, options and defaults, then feed that file to Pashua, and Pashua returns the user input in standard out. Just what you would like to have for you command-line PHP script.

Pashua works only in Mac OS X, so it is not a general solution like the GTK bindings for PHP. But it is a good alternative for doing command-line PHP scripting on Panther. The distribution comes with examples in different languages, so you can easily see how to make use of this tool.

Just for the fun of it, I’ve written a set of wrapper classes for Pashua, so you can create the configuration file programatically, and then run Pashua and get the user input information as an associative array.

GUI generated from the example code

To use the utility classes it can be as simple as the code below, which will generate the GUI above:

  require_once 'Pashua.php';
  // create a configuration instance
  $c = new PashuaConfiguration();
  $c->setWindowTitle('Pashua example');
  $c->setTransparency(0.75);
  // create an image
  $i = new PashuaImage('pear');
  $i->setLabel('http://pear.php.net');
  $i->setTooltip('Cool packages by crazy people');  $i->setPath('pear-power.gif');
  // and some radio buttons
  $p = new PashuaRadiobutton('favorite');
  $p->setLabel('What is your favorite Math package?');
  $p->setTooltip('Yes, a *Math* package');
  $p->addOption('Math_Matrix');
  $p->addOption('Math_Stats');
  $p->addOption('Math_Histogram');
  $p->addOption('Math_Vector');
  $p->addOption('Math_Quaternion');
  $p->addOption('Math_Fibonacci');
  $p->addOption('Math_Integer');
  $p->addOption('Math_Complex');
  $p->setDefault('Math_Quaternion');
  // finally a login dialog
  $phish = new PashuaText('phish');
  $phish->setText('PEAR login?');
  $u = new PashuaTextfield('username');
  $u->setLabel('Username:');
  $u->setTooltip('Trust me, put your PEAR username');
  $u->setWidth(120);
  $w = new PashuaPassword('password');
  $w->setLabel('Password:');
  $w->setTooltip("Hey!, you trusted me with your username already, didn't you?");
  $w->setWidth(120);
  // add all the items to the configuration  $c->addElement($i);
  $c->addSeparator();
  $c->addElement($p);
  $c->addSeparator();
  $c->addElement($phish);
  $c->addElement($u);
  $c->addElement($w);
  // create the dialog and get the user input  $e = new Pashua();
  $e->setConfiguration($c);
  $ret = $e->run();
  // do something with the user input

In the end you will have the user values in $ret which your would use in the rest of your script. One important thing is that because the wrapper class needs to run an external program (Pashua), if that is disabled in your PHP installation, then nothing will work.

Apart from the GUI elements listed in the example above, there are more elements you can use to make a nice dialog.

This is a really nice way to make your command-line PHP script look and feel at home in Mac OS X.

Note: the wrapper classes and the example are proofs of concept and work with Pashua 0.9. It is not very likely that I’ll maintain up to date versions as new Pashua releases come along. But this being Open Source, and the PHP License being very permissive, you can always do the improvements yourself.

I’ve put the utility classes and the example above in a zip file. Get it and play with the sample code.