Mac in the Shell: Pashua: Helping the GUI Crowd

Give your shell scripts a GUI

by Edward Marczak

Introduction

In the August 2006 issue of MacTech, I wrote an Mac in the Shell: Pashua: Helping the GUI Crowd, "GUI-up Your Script." It talked about ways to add a GUI to your shell script using AppleScript Studio, part of XCode. As we saw in that article, now on-line at http://www.mactech.com/articles/mactech/
Vol.22/22.08/GUI-upyourScript/index.html, there are pros and cons to the shell scripter using this method. Since a year is an eternity in technology-time, I'm back to talk about some notable alternatives. My current fave? Pashua. Read on to see how you can use Pashua to add a GUI to your shell script.

What's Happening

The reasons for creating a GUI for a shell script remain the same: you're a shell scripter, but the person running the script is a GUI-type. Perhaps it's just a nicer way to ask for input. In any case, it's a powerful combination.

Before I get into Pashua, I'd like to mention some alternatives. Platypus (http://www.sveinbjorn.org/platypus) is a wrapper that will take a shell script and construct the necessary bundle structure to make the script a 'regular' double-clickable OS X ".app". Useful, in some cases, to create 'droplets' and to allow end-users to run shell scripts at login time via their Login Items. (Though, we'd never do that as good admins, we'd use launchd, right?). CocoaDialog (http://cocoadialog.sourceforge.net/), true to its name, allows the shell scripter to create dialogs. Not quite full windows. Think "error dialog" or "progress dialog." However, many times, that's precisely what you need, so, CocoaDialog exists, says what it does, and does a good job doing what it says.

However, Pashua is a bit more. Pashua will let you construct full windows, using most common GUI widgets. The best thing about Pashua is the ease in which you can get data in and out of the windows you create. Let's get started!

Good Times

First, go download Pashua itself at http://www.bluem.net/downloads/pashua_en/. From there, drop the distribution someplace logical (to your situation). We're shell people, right? So, get into Terminal, if you're not there already, and change into the new Pashua folder. From there, we'll dig into the Pashua.app bundle. Really, all you need to know is that from the shell you'll need the Pashua binary, located at Pashua.app/Contents/MacOS/Pashua. You may want to get this into your current $PATH so you can simply type Pashua from now on, but I'll leave that up to you. Go on and run that if you like - you'll receive the dialog show in figure 1.

Figure 1 - Ah, we need a config file.

As evidenced by the error dialog, Pashua is driven entirely by a config file. The config file is nothing esoteric - it's simply a text description that describes the window layout. Let's start with the easiest definition. Fire up your favorite text editor, and enter this:

Save it as win_test.pash. This tells Pashua that we want a new element named "uname" to be created. The "uname" element will be a text field. It will have a label above the text field that says, "Enter the user ID", and will have default text of "User ID". Finally, we create the width of the field to be 340 pixels.

Enough chit-chat - let's run it. If you did get Pashua into your path, simply type:

Pashua win_test.pash

(If you didn't get it into your path, specify the full path to the Pashua binary instead. Something like: /Applications/Pashua/Pashua.app/Contents/MacOS/Pashua).

...and we're greeted with this:

Figure 2 - Our "Hello World" example.

Now, press the return key, or click the "OK" button. Notice what happens in the shell:

$ Pashua win_test.pash
uname=User ID

That's right! Pashua simply takes the values of all elements and hands them to you on standard out. For shell scripters, this is a relief! Standard in and standard out: that's how shell scripting works! If you've tried to interface with AppleScript, you'll know that passing values back and forth is a huge pain in the patoot. Pashua is a shell-scripters GUI tool more than a GUI tool that simply has 'support' for scripting languages. So, onward!

Let's go for some eye candy. I'm going to put the MacTech logo in the header, and name the Window. Insert this above the lines already in win_test.pash:

...and again, we display our window (Pashua win_test.pash) and see the results in figure 3.

Figure 3 - Ooooooh, fancy

After clicking OK, note that our output hasn't changed, as we haven't added any elements that accept input. You probably picked up that the hash mark ("#") denotes a comment, and Pashua will ignore any line beginning with one. Additionally, the "*" "element" affects the entire window itself. In addition to the title, so it's not "Pashua", you can also change the following window elements:

appearance: Only allowed value is metal, which will create a "brushed metal" window. Under Leopard, "metal" creates a single, large "unified" window.

autoclosetime: If autoclosetime is set to an integer number larger than 1, the dialog will automatically close after the specified number of seconds have passed. Note that if a window auto-closes, no values are passed back.

floating: Setting floating to 1 will result in the window floating above other windows. (Note: does not work on Mac OS X 10.2)

x: Sets the horizontal position where the window should be opened on the screen (0 is the left border of the screen)

y: Sets the vertical position where the window should be opened on the screen (0 is the upper border of the screen)

Frankly, "title" is about all I use, as I like the auto-positioning on the screen, and I never seem to have need of the other attributes, but you may.

If you want to let a user bail out of an action, add a cancel button to the window:

Run our test window with this in the config file, but note the output when you cancel:

$ Pashua win_test.pash
cb=1

If the cancel button is invoked - either by clicking or pressing the escape key, all values are discarded, and only the cancel button variable is returned.

The Love Boat

How about getting data in and out of our Window? Let's look at a few practical examples, starting with getting data out. We've already seen that Pashua returns its data on standard out. For the sake of it, let's add one more element, a checkbox. Add these lines beneath the textfield definition:

Save, and now check out our window. Enter some data, and click the "OK" button. Notice the output:

$ Pashua win_test.pash
uname=User ID
alter_account=1
cb=0

While that may be great for viewing on standard out in the shell itself, it doesn't help us too much in a script. To handle this gracefully, we really need to be in a script, which is where we want to be anyway. Let's look at a small, semi-robust wrapper script:

Save this a pwrapper.sh, and call it like this, with our sample window:

./pwrapper win_test.pash

Enter some data and click "OK". What happened? Seemingly nothing. No apparent output from Pashua. What's actually happening is that our wrapper script is capturing the output and assigning it to variables within the script. Those variables are now available to us directly. Now, the value passed back from uname is available in "$uname". While this wrapper script solves many problems for us, it doesn't go the distance if we need to dynamically create windows. For that, we need to use a little string creation and concatenation.

Fantasy Island

Following this article is a "complete" script that gathers local user accounts for a fictitious migration/alteration (Listing 1). The idea here is to show off Pashua, and not come up with a user-altering utility. The new bit here is that we create a drop-down menu that we populate dynamically at runtime.

The final window appears in figure 4. Note that the only output is generated purposefully by our script:

$ ./pwrapper.sh
Converting user marczak
Also altering account

Figure 4 - Window with drop-down menu, generated dynamically.

The Gong Show

While this but scratches the surface of all that you can do with Pashua, I hope it was a helpful introduction that kick-starts your thoughts (and typing fingers!). Read the documentation included with the download for other elements that can be put into a window. Also, in the Examples folder, you'll find an application bundle that shows how to create a double-clickable app - all using your shell script and Pashua. If you're not a bash scripter, remember, Pashua accepts a text file as input, and sends its output via standard out. That's very friendly to any scripting language - Perl, Python, Ruby and more. There's even an AppleScript example.

Media of the month: Project management is my focus this month, and there are a lot of good books out there. Think about everything we do, and how many we could classify as "projects." Peruse your local bookstore's shelf for a project management title that suits you. I'm currently finishing up "The Zen Approach to Project Management: Working From Your Center to Balance Expectations and Performance" by George Pitagorsky. The "Zen" part of this title isn't to be taken lightly, and isn't being used as a fashion piece as many others do. This book won't teach you project management techniques per se, but it will teach you, using Zen "centering" exercises how to see the big picture when managing a project. Highly recommended, but only after you have some project management knowledge.

Here comes Macworld! One month away. I'll be presenting two (maybe three) topics this year. I'm leading a hands-on lab on Wednesday that teaches how to get started using the shell. So many people don't even know where to start at all, but know there's power at that cursor starting back at them. I'll also be presenting OS X Server Collaboration Tools, also on Wednesday along with Ben Greisler. This will talk about all the new features in Leopard server like iCal server, the new Wiki and more. Finally, I may be sitting in with Schoun Regan to talk about Kerberos on Tuesday in the Power Tools session, which will be informative if I'm there or not. Of course, don't miss the MacTech booth, where I'll be popping up at from time to time. Hope to see everyone in San Francisco!