Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'An AppleScript to mount, run, unmount a disk image' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to mount, run, unmount a disk image
Authored by: jbreazeale on May 21, '05 04:13:54AM
I tried to get the AppleScript working. I could get the disk image to mount and the application to start but after the application quit the disk image was still mounted. I came up with a different solution that works pretty well for my needs. What I wanted was a way to avoid having my children handle CDs and launching applications. To solve this problem I built bootstrap AppleScript application icon and name set to be the same as the program that it ultimately launches. The AppleScript application calls a the mntrun shell script that adds canned, location-specific text to the application and disk image names then mntrun calls the script that does the real work, mntlauncher. Mntlauncher mounts the disk drive image, starts the application, waits for the application to finish, then unmounts the disk image. Here's the AppleScript that launches "Reader Rabbit's Toddler":

property launcher : "/Applications/Launchers/mntrun"
property itemname : "The Learning Company/Reader Rabbit Toddler/Reader Rabbit's Toddler"
property diskpath : "Reader Rabbit Toddler.img"

on run
	tell application "Finder"
		do shell script (launcher & "  \"" & itemname & "\" \"" & diskpath & "\"")
	end tell
end run
Here's the text of /Applications/Launchers/mntrun:

#!/bin/bash

#
# mntrun
#
# Front end script that accepts an application name and disk image name
# and calls mntlaunch.  This script is an intermediary to mntrun.
#
launcher=/Applications/Launchers/mntlaunch

itemname="$1"
diskpath="$2"

#
# Customize these two lines to launch applications from a standard
# location and to find disk images in a standard location.
#
application_path="/Applications/$itemname"
fulldiskpath="/Volumes/160GB HD/CD Cache/Educational/originals/$diskpath"

#
# Call the launcher script!
#
$launcher "$application_path" "$fulldiskpath"
Here's the text of /Applications/Launchers/mntlaunch:

#!/bin/bash

#
# mntlaunch
#
# This script will mount a volume image file, run an application, then
# will unmount the mounted volume.  The inputs are full path names to
# the application and image file.
#

function cleanup
{
    rm -f /tmp/$PROG.*.$$
}

PROG=`basename $0`
TMPFILE1=/tmp/$PROG.1.$$

trap 'cleanup; exit 1' 1 2 3 15
itempath="$1"
volume="$2"

#
# Collect the volume path as the disk image is mounted
#
export COLUMNS=1024
hdiutil attach "$volume" -mount required -noverify 2>&1 >$TMPFILE1
return_code=$?
if [ $return_code -ne 0 ]; then
    echo "$PROG: Mount failed, status=$return_code" >&2
    exit $return_code
fi

# 
# Disk image mounted!  Launch application.
#
open "$itempath"
return_code=$?
if [ $return_code -ne 0 ]; then
    echo "$PROG: Open failed, status=$return_code" >&2
    exit $return_code
fi

#
# Application launched.  Wait for application to exit.
#
sleep 1
while ((1))
do
    #
    # Count to see how many times the application appears.  If does
    # appear one time there must be a problem or it has exited.
    #
    count=`ps -ax | grep "$itempath" | grep -v grep | wc -l`
    if [ $count -ne 1 ]; then
        break
    fi
    sleep 1
done

#
# Get the full path name of the mounted disk image.
#
disk=`grep ^/dev/disk $TMPFILE1 | cut -d' ' -f1`

#
# Unmount the disk image.
#
hdiutil unmount $disk -force -quiet
hdiutil detach $disk -force -quiet

cleanup
In mntrun you can see the script is designed for applications to be located in the /Applications folder. The AppleScript application merely needs to make reference to the application path relative to /Applications. In mntrun you can also see that I keep my disk images in /Volumes/160GB HD/CD Cache/Educational/originals, so mntrun creates the proper full path name to the disk image for mntlaunch. If the user wishes to run an application on the mount volume one merely needs to refer to an application path such as "../Volumes/Volume Name/Application Path". The "../Volumes/Volume Name" is the path to the mounted volume and "Application Path" is the path to the application within the mounted volume. It's all quite a hack, but at least I can stop having to make CD copies for my children (3 and 6 years old)!

[ Reply to This | # ]