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

Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »

Price Scanner via MacPrices.net

New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more
Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... Read more

Jobs Board

DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple 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
IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.