How to create an OS X application with bash.

About a year ago I started working on a project to help new hired employees set up their new macbooks for use on our network. I knew I wanted to present the setup as an application that started as soon as they logged in to their macbook for the first time. I came up with this list of tasks it would have to perform.

  • 1. The app needed to start up as soon as the user logged in for the first time and I did not want the user to be able to skip the setup.
  • 2. It needed to be able to rename the machine specifically for the user according to our naming convention.
  • 3. It would also need to unbind from Active Directory and then rebind with the new name.
  • 4. The app would need to install an 802.1x wireless configuration profile so that it could be used on our network.
  • 5. It would have to walk the user through a basic tutorial and show them how to use our OnDemand portal to install software from our Absolute Manage servers.
  • 6. The app needed to have as little interaction from the user as possible.
  • 7. Lots and lots of error checking!

So how was I going to create an app that launched at startup, pre-populated the user’s information from Active Directory, created a properly formatted machine name from this information, bind to Active Directory, install an 802.1x configuration profile, walk the user through a brief tutorial and delete itself when it was all finished?

I did some research and since I am still learning xcode I decided to go with Pashua for the GUI aspect of the app since I could script it all in bash.

The latest version of Pashua can be found here.

Once I downloaded Pashua and read through the documentation, I started scripting the main part of the application in bash.

Here is the first part of the code that defines pashua’s run function.

#!/bin/bash
pashua_run() {
 
# Write config file
pashua_configfile=`/usr/bin/mktemp /tmp/pashua_XXXXXXXXX`
echo "$1" > $pashua_configfile
 
# Find Pashua binary. We do search both . and dirname "$0"
# , as in a doubleclickable application, cwd is /
# BTW, all these quotes below are necessary to handle paths
# containing spaces.
bundlepath="Pashua.app/Contents/MacOS/Pashua"
if [ "$3" = "" ]
then
mypath=`dirname "$0"`
for searchpath in "$mypath/Pashua" "$mypath/$bundlepath" "./$bundlepath" \
"/Applications/$bundlepath" "$HOME/Applications/$bundlepath"
do
if [ -f "$searchpath" -a -x "$searchpath" ]
then
pashuapath=$searchpath
break
fi
done
else
# Directory given as argument
pashuapath="$3/$bundlepath"
echo "$pashuapath"
fi
 
if [ ! "$pashuapath" ]
then
echo "Error: Pashua could not be found"
exit 1
fi
 
# Manage encoding
if [ "$2" = "" ]
then
encoding=""
else
encoding="-e $2"
fi
 
# Get result
result=`"$pashuapath" $encoding $pashua_configfile | sed 's/ /;;;/g'`
 
# Remove config file
rm $pashua_configfile
 
# 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
}
fi

Following the main run function I needed an easy way to get the current working directory of the app so I created the DIR variable below. This way we can easily access the files we will bundle with the app.

DIR="$( cd "$( dirname "$0" )" && pwd )"

Now lets create our first window that welcomes the user to the setup application. I will call it WelcomeScreen and you can see how it is defined below.

#!/bin/bash
 
WelcomeScreen="
 
*.title = LISD Macbook Setup
*.title.width = 400
 
img.type = image
img.path = $DIR/LogoTrans.png
 
img.maxwidth = 250
img.x = 0
img.y = 175
img.border = 0
 
welcome.type = text
welcome.width = 275
welcome.x = 100
welcome.y = 200
welcome.default = Welcome to the MacBook Setup Wizard.
 
welcome1.type = text
welcome1.width = 400
welcome1.x = 0
welcome1.y = 50
welcome1.default = This application will walk you through setting up your MacBook for use on our network. Once you have completed the setup, your MacBook will be personalized for you and your email will be setup automatically when you first launch Outlook.[return][return]For technical assistance please contact the helpdesk at...
 
db.type = defaultbutton
db.label = Next
"
pashua_run "$WelcomeScreen"
 
if [ "$back" = "1" ]
then
nohup "$DIR/Macbook Setup" &
exit 0
fi

And here is what the welcome screen window looks like.

 

Welcome screen window
Welcome screen window

 

To be continued…

Leave a Reply

Your email address will not be published. Required fields are marked *

*