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

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.