TweetFollow Us on Twitter

Mac in the Shell: Pashua: Helping the GUI Crowd

Volume Number: 23 (2007)
Issue Number: 12
Column Tag: Mac in the Shell

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:

uname.type = textfield
uname.label = Enter the user ID
uname.default = User ID
uname.width = 340

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:

# Set the window title
*.title=MacTech Utility
# Display an image
img.type = image
img.path = /Users/Shared/images/mt_logo.gif
img.border = 0

...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:

transparency: Sets the window's transparency, decimal value from 0 (invisible) to 1 (opaque)

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:

cb.type = cancelbutton
cb.label = Cancel
cb.tooltip = Closes this window without taking action

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:

alter_account.type = checkbox
alter_account.label = Alter Account?
alter_account.tooltip = Checking this box will alter the account

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:

#!/usr/bin/env bash
if [ -N $1 ]; then
        echo "You need to supply the Pashua config file";
        exit 1
fi
result=`/Users/marczak/Applications/Pashua/Pashua.app/Contents/MacOS/Pashua $1 | sed 's/ /;;;/g'`
# Parse result
for line in $result
do
        key=`echo $line | sed 's/^\([^=]*\)=.*$/\1/'`
        value=`echo $line | sed 's/^[^=]*=\(.*\)$/\1/' | sed 's/;;;/ /g'`
        varname=$key
        varvalue="$value"
        eval $varname='$varvalue'
done

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!


Ed Marczak owns and operates Radiotope, a technology consulting practice. Think technology is dy-no-myte? Find out more at http://www.radiotope.com

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links... | Read more »
Price of Glory unleashes its 1.4 Alpha u...
As much as we all probably dislike Maths as a subject, we do have to hand it to geometry for giving us the good old Hexgrid, home of some of the best strategy games. One such example, Price of Glory, has dropped its 1.4 Alpha update, stocked full... | Read more »
The SLC 2025 kicks off this month to cro...
Ever since the Solo Leveling: Arise Championship 2025 was announced, I have been looking forward to it. The promotional clip they released a month or two back showed crowds going absolutely nuts for the previous competitions, so imagine the... | Read more »
Dive into some early Magicpunk fun as Cr...
Excellent news for fans of steampunk and magic; the Precursor Test for Magicpunk MMORPG Crystal of Atlan opens today. This rather fancy way of saying beta test will remain open until March 5th and is available for PC - boo - and Android devices -... | Read more »
Prepare to get your mind melted as Evang...
If you are a fan of sci-fi shooters and incredibly weird, mind-bending anime series, then you are in for a treat, as Goddess of Victory: Nikke is gearing up for its second collaboration with Evangelion. We were also treated to an upcoming... | Read more »
Square Enix gives with one hand and slap...
We have something of a mixed bag coming over from Square Enix HQ today. Two of their mobile games are revelling in life with new events keeping them alive, whilst another has been thrown onto the ever-growing discard pile Square is building. I... | Read more »
Let the world burn as you have some fest...
It is time to leave the world burning once again as you take a much-needed break from that whole “hero” lark and enjoy some celebrations in Genshin Impact. Version 5.4, Moonlight Amidst Dreams, will see you in Inazuma to attend the Mikawa Flower... | Read more »
Full Moon Over the Abyssal Sea lands on...
Aether Gazer has announced its latest major update, and it is one of the loveliest event names I have ever heard. Full Moon Over the Abyssal Sea is an amazing name, and it comes loaded with two side stories, a new S-grade Modifier, and some fancy... | Read more »
Open your own eatery for all the forest...
Very important question; when you read the title Zoo Restaurant, do you also immediately think of running a restaurant in which you cook Zoo animals as the course? I will just assume yes. Anyway, come June 23rd we will all be able to start up our... | Read more »
Crystal of Atlan opens registration for...
Nuverse was prominently featured in the last month for all the wrong reasons with the USA TikTok debacle, but now it is putting all that behind it and preparing for the Crystal of Atlan beta test. Taking place between February 18th and March 5th,... | Read more »

Price Scanner via MacPrices.net

AT&T is offering a 65% discount on the ne...
AT&T is offering the new iPhone 16e for up to 65% off their monthly finance fee with 36-months of service. No trade-in is required. Discount is applied via monthly bill credits over the 36 month... Read more
Use this code to get a free iPhone 13 at Visi...
For a limited time, use code SWEETDEAL to get a free 128GB iPhone 13 Visible, Verizon’s low-cost wireless cell service, Visible. Deal is valid when you purchase the Visible+ annual plan. Free... Read more
M4 Mac minis on sale for $50-$80 off MSRP at...
B&H Photo has M4 Mac minis in stock and on sale right now for $50 to $80 off Apple’s MSRP, each including free 1-2 day shipping to most US addresses: – M4 Mac mini (16GB/256GB): $549, $50 off... Read more
Buy an iPhone 16 at Boost Mobile and get one...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering one year of free Unlimited service with the purchase of any iPhone 16. Purchase the iPhone at standard MSRP, and then choose... Read more
Get an iPhone 15 for only $299 at Boost Mobil...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering the 128GB iPhone 15 for $299.99 including service with their Unlimited Premium plan (50GB of premium data, $60/month), or $20... Read more
Unreal Mobile is offering $100 off any new iP...
Unreal Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering a $100 discount on any new iPhone with service. This includes new iPhone 16 models as well as iPhone 15, 14, 13, and SE... Read more
Apple drops prices on clearance iPhone 14 mod...
With today’s introduction of the new iPhone 16e, Apple has discontinued the iPhone 14, 14 Pro, and SE. In response, Apple has dropped prices on unlocked, Certified Refurbished, iPhone 14 models to a... Read more
B&H has 16-inch M4 Max MacBook Pros on sa...
B&H Photo is offering a $360-$410 discount on new 16-inch MacBook Pros with M4 Max CPUs right now. B&H offers free 1-2 day shipping to most US addresses: – 16″ M4 Max MacBook Pro (36GB/1TB/... Read more
Amazon is offering a $100 discount on the M4...
Amazon has the M4 Pro Mac mini discounted $100 off MSRP right now. Shipping is free. Their price is the lowest currently available for this popular mini: – Mac mini M4 Pro (24GB/512GB): $1299, $100... Read more
B&H continues to offer $150-$220 discount...
B&H Photo has 14-inch M4 MacBook Pros on sale for $150-$220 off MSRP. B&H offers free 1-2 day shipping to most US addresses: – 14″ M4 MacBook Pro (16GB/512GB): $1449, $150 off MSRP – 14″ M4... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.