anyone good with applescript?

Soldato
Joined
19 Oct 2002
Posts
3,480
Hi guys,

been trying to get my head around applescript and as an exercise i'm trying to make a loader for diablo II (which will also work for warcraft 3 and starcraft and anything else which requires a cd image to be mounted prior to running the game)

so far i have this:

Code:
property gameRunning : false
property gameMounted : false
property gameName : "Diablo II (Carbon)"
property gameImageName : "Lord of Destruction"

set gameImage to (path to library folder from user domain as text) & "Installation Files & Disc Images:Games:Diablo II:Diablo II - Lord Of Destruction.iso"
do shell script "hdiutil attach " & quoted form of POSIX path of gameImage
activate application gameName

on idle
	tell application "System Events"
		set gameRunning to gameName is in (get name of processes)
		set gameMounted to gameImageName is in (get name of disks)
	end tell
	if gameRunning is false and gameMounted then quit
	return 60
end idle

on quit
	do shell script "hdiutil detach " & quoted form of ("/Volumes/" & gameImageName)
	continue quit
end quit

and this does actually work, but only 10% of the time...

when it works, the image mounts, the game runs, and when the game is exited, the disc unmounts and the script app closes...

however, when it doesnt work (which like i said is the majority of the time) the script loads, hdiutil loads (activity viewer) and maxes out the cores and it remains like that until i force quit it...

so i'm assuming there is something unorthodox about the way the image is getting mounted... any advice on why this happens or general applescript hints and tips related to this would be much appreciated :)
 
An AppleScript to mount, run, unmount a disk image
Tue, Sep 7 '04 at 9:11AM PDT

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:

* diskname: should be the name you see under the icon for the disk image on your desktop.
* diskpath: should be the full path to the disk image file on your hard disk. Get this by selecting the file, press Command-I for Get Information, and combine the "Where" string with the "Name and extension" string.
* itemname: should be the full path of the application program that the script should run.
* appname: should be the name of the application itself (without the path).

As I said, this works here, but I'll be grateful for fixes and improvements.

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


This was literally my first search result on Google :p
 
Last edited by a moderator:
i had also found that mate, and was one of the things i tried before i tried to construct my own...

the problem still exists in this version (maybe a leopard thing?), so the question still remains, is there something in my code (or this new code for that matter), that could be maxing out hdiutil?
 
Looks like it's a post 10.3.x thing

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.
 
Back
Top Bottom