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

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 »
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 below... | Read more »
Marvel Future Fight celebrates nine year...
Announced alongside an advertising image I can only assume was aimed squarely at myself with the prominent Deadpool and Odin featured on it, Netmarble has revealed their celebrations for the 9th anniversary of Marvel Future Fight. The Countdown... | Read more »
HoYoFair 2024 prepares to showcase over...
To say Genshin Impact took the world by storm when it was released would be an understatement. However, I think the most surprising part of the launch was just how much further it went than gaming. There have been concerts, art shows, massive... | Read more »

Price Scanner via MacPrices.net

AT&T has the iPhone 14 on sale for only $...
AT&T has the 128GB Apple iPhone 14 available for only $5.99 per month for new and existing customers when you activate unlimited service and use AT&T’s 36 month installment plan. The fine... Read more
Amazon is offering a $100 discount on every M...
Amazon is offering a $100 instant discount on each configuration of Apple’s new 13″ M3 MacBook Air, in Midnight, this weekend. These are the lowest prices currently available for new 13″ M3 MacBook... Read more
You can save $300-$480 on a 14-inch M3 Pro/Ma...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
24-inch M1 iMacs available at Apple starting...
Apple has clearance M1 iMacs available in their Certified Refurbished store starting at $1049 and ranging up to $300 off original MSRP. Each iMac is in like-new condition and comes with Apple’s... Read more
Walmart continues to offer $699 13-inch M1 Ma...
Walmart continues to offer new Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBook for sale by... Read more
B&H has 13-inch M2 MacBook Airs with 16GB...
B&H Photo has 13″ MacBook Airs with M2 CPUs, 16GB of memory, and 256GB of storage in stock and on sale for $1099, $100 off Apple’s MSRP for this configuration. Free 1-2 day delivery is available... Read more
14-inch M3 MacBook Pro with 16GB of RAM avail...
Apple has the 14″ M3 MacBook Pro with 16GB of RAM and 1TB of storage, Certified Refurbished, available for $300 off MSRP. Each MacBook Pro features a new outer case, shipping is free, and an Apple 1-... Read more
Apple M2 Mac minis on sale for up to $150 off...
Amazon has Apple’s M2-powered Mac minis in stock and on sale for $100-$150 off MSRP, each including free delivery: – Mac mini M2/256GB SSD: $499, save $100 – Mac mini M2/512GB SSD: $699, save $100 –... Read more
Amazon is offering a $200 discount on 14-inch...
Amazon has 14-inch M3 MacBook Pros in stock and on sale for $200 off MSRP. Shipping is free. Note that Amazon’s stock tends to come and go: – 14″ M3 MacBook Pro (8GB RAM/512GB SSD): $1399.99, $200... Read more
Sunday Sale: 13-inch M3 MacBook Air for $999,...
Several Apple retailers have the new 13″ MacBook Air with an M3 CPU in stock and on sale today for only $999 in Midnight. These are the lowest prices currently available for new 13″ M3 MacBook Airs... Read more

Jobs Board

*Apple* Systems Administrator - JAMF - Syste...
Title: Apple Systems Administrator - JAMF ALTA is supporting a direct hire opportunity. This position is 100% Onsite for initial 3-6 months and then remote 1-2 Read more
Relationship Banker - *Apple* Valley Financ...
Relationship Banker - Apple Valley Financial Center APPLE VALLEY, Minnesota **Job Description:** At Bank of America, we are guided by a common purpose to help Read more
IN6728 Optometrist- *Apple* Valley, CA- Tar...
Date: Apr 9, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92308 **Requisition ID:** 824398 At Target Optical, we help people see and look great - and Read more
Medical Assistant - Orthopedics *Apple* Hil...
Medical Assistant - Orthopedics Apple Hill York Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.