Author Topic: Detect Application Close with AppleScript  (Read 217 times)

Offline Canal Noises

  • Member
  • *
  • Posts: 4
  • New Member
Detect Application Close with AppleScript
« on: July 04, 2017, 10:46:25 AM »
I have a couple dozen old games that I'm working on getting set up in OS 9, and I've been using AppleScript to simplify the process of launching them. Many of the games require the CD to be mounted, so I have my script mount a disk image, set the screen resolution and color depth if needed, and then launch the game.

That's all pretty simple to script, but when the application is closed I'd like to have the screen resolution and color depth set back to normal and have the disk image unmounted—basically have the script clean up after itself. I've come across a few ways to accomplish that on OS X, but nothing on the classic systems. Does anyone know if it's possible to do what I'm wanting to do?

Offline IIO

  • Platinum Member
  • *****
  • Posts: 1548
  • new to the forums
Re: Detect Application Close with AppleScript
« Reply #1 on: July 04, 2017, 12:41:06 PM »
"unmount volume" on MacOS9 is equal to "move to trash". i think if you want to track the name of the opened image for later unmounting you would have to leave the applescript runtime open while you run the game...?
"It is true that the "pre-emptive multitasking" advantage present in OS X can be illustrated by downloading CD-ROM ISOs and rendering chaos theory formulas while simultaneously instant messaging and posting on FaceBook what you ate... but in reality, what did you create?"
- DieHard, random forum troll at macos9lives.com

Offline Canal Noises

  • Member
  • *
  • Posts: 4
  • New Member
Re: Detect Application Close with AppleScript
« Reply #2 on: July 04, 2017, 01:52:20 PM »
I've actually got the disk ejecting code down, I think. If the name of the disk is known it's simple to unmount/eject it:
Code: [Select]
tell application "Finder" to eject disk "Disk Name"
What I'm stuck on is how to have that statement triggered when the game quits. What I'm thinking is that if I leave the script running I can have it periodically check to see if the game is still running, and then only move on to eject the disk when it no longer sees that process.

I think I may have actually found some commands that I can use to make this work. I'm going to try it out and I'll report back. Thanks for the reply, IIO :)

Offline Canal Noises

  • Member
  • *
  • Posts: 4
  • New Member
Re: Detect Application Close with AppleScript
« Reply #3 on: July 04, 2017, 02:56:40 PM »
Got it! Here's my script, in case it can help anyone else do the same thing.

Summary of what it does:
  • Stores the current display settings in a variable (monitorSettings) using the "screen list" command from Jon's Commands
  • Mount the disk image
  • Set screen resolution and color depth as required by the app
  • Launch the app
  • Waith for the app to quit
  • Revert to the display settings in use when the script started (Using "set screens" from Jon's Commands)
  • Eject the disk image
Code: [Select]
set monitorSettings to screen list starting with main screen --Store current display settings

tell application "Finder"
open file "Macintosh HD:Applications (Mac OS 9):Disk Image.iso" --Mount the CD image
set screens to {screen size:{640, 480}, color depth:8} --Set the display resolution and color depth (640x480 at 256 colors)
open file "Mounted Disk Image:App Name" --Launch app
end tell

--Watch for app to quit
set appRunning to "1"
repeat until appRunning is "0"
tell application "Finder"
if exists process "App Name" then
set appRunning to "1"
else
set appRunning to "0"
end if
end tell
end repeat

set screens to monitorSettings --Revert to original display settings

--Eject the CD image:
tell application "Finder"
eject disk "Mounted Disk Image"
end tell

Offline IIO

  • Platinum Member
  • *****
  • Posts: 1548
  • new to the forums
Re: Detect Application Close with AppleScript
« Reply #4 on: July 05, 2017, 02:04:44 AM »
>>periodically check to see if the game is still running

yeah, classic workaround for stuff like that. thanks for sharing your results.
"It is true that the "pre-emptive multitasking" advantage present in OS X can be illustrated by downloading CD-ROM ISOs and rendering chaos theory formulas while simultaneously instant messaging and posting on FaceBook what you ate... but in reality, what did you create?"
- DieHard, random forum troll at macos9lives.com