A number of hints (and newsgroup postings elsewhere) have addressed the problem of creating a script that mounts a disk image, runs an application that uses data from the image, and then unmounts the image after the application quits. By far the best was one was in a comment from Puffyn in a thread here last year, but it never quite worked on my system.
The problem I kept having was that the script would work perfectly the first time I ran it, but the second and all later times (until a reboot), the script would mount the disk image and launch the application that uses the image -- but the application would not detect the image, and ask me to mount it. The application was a Classic app (OED2 CD-ROM), which may or may not have had something to do with it. OED2 CD-ROM is the Oxford English Dictionary software, and its data needs to be on a CD-ROM or disk image, apparently.
Under Panther (or maybe under OS X in general), it seems that it's not easy for a script to unmount a disk so cleanly that it can be mounted a second time in a way that all applications can see it. I found that the script needs to unmount the disk (apparently with a "force" switch for the cleanest possible rsult) AND detach the corresponding device (disk1, etc.) in OS X's list of devices.
Anyway, with the help of many people on the forums (notably Dale Mox and jbc), I finally put together a script that seems to work perfectly, at least on my system. The last problem that needed solving was determining the name of the device on the fly, because the disk number of the image will be different at different times, depending on whether or not you've mounted USB or Firewire disks, etc. Dale Mox solved that one for me.
Here's the code. It will need to be customized to run on your system, but all the changes that need to be made are in the declarations at the top. Here are the explanations of the variables:
property diskname : "OED2"
property diskpath : "Macintosh HD:Applications (Mac OS 9):OED2 program:OED2.dmg"
property itemname : "Macintosh HD:Applications (Mac OS 9):OED2 program:OED2 CD-ROM"
property appname : "OED2 CD-ROM"
on run
tell application "Finder"
if not (exists the disk diskname) then
do shell script ("hdiutil attach " & quoted form of ¬
POSIX path of (diskpath as string) & " -mount required")
repeat until name of every disk contains diskname
delay 1
end repeat
end if
open item itemname
end tell
delay 5
end run
-- remainder based on hint by puffyn at macosxhints.com:
-- hint named "Quick Applescript to Mount Disk Image"
-- shell script to get devname by Dale Mox with fixes by jbc
on idle
set devname to do shell script "mount | grep " & diskname & ¬
" | cut -f1-1 -d \" \" | cut -f3-3 -d \"/\""
tell application "Finder"
if not (exists the disk diskname) then
return
else
set x to the name of every process
if appname is not in x then
do shell script ("hdiutil unmount /Volumes/" & diskname & " -force")
do shell script ("hdiutil detach " & devname & " -force")
tell me to quit
end if
end if
end tell
return 2
end idle
Thanks in advance for any improvements.
Mac OS X Hints
http://hints.macworld.com/article.php?story=20040905112951299