AppleScript to launch app, shut down when closing app

About SheepShaver, a PPC Mac emulator for Windows, MacOS X, and Linux that can run System 7.5.3 to MacOS 9.0.4.

Moderators: Cat_7, Ronald P. Regensburg, ClockWise

Post Reply
emendelson
Forum All-Star
Posts: 1706
Joined: Tue Oct 14, 2008 12:12 am

AppleScript to launch app, shut down when closing app

Post by emendelson »

There are times when I want to run only a single program in SheepShaver or Basilisk II. Here's an AppleScript that can be put into the Startup Items folder in the System Folder in a SS/BII system and it will do this:

When the emulator starts up, the script will launch the application you specify in the script. When you close the application, the emulator will shut down. The first time you run it, but only the first time, it will ask you where the application is located.

To create this script, copy the code below into the AppleScript editor (you may need to install it separately) inside the SheepShaver/Basilisk II system. When saving, set Kind to Application; add checkmarks next to "Stay Open" and "Never Show Startup Screen."

Test it by launching it OUTSIDE the Startup Items folder before actually putting it in the Startup Items folder.

This works in System 7.55 or later. I know there are more efficient ways of writing it, but the more efficient ways don't seem to work in ancient Macs. (For example, "delay" doesn't exist in system 7.5.5, so I used a kludge workaround instead.)

One important thing: Make absolutely certain that the application name specified in two parts of the script is exactly the same as the name of the application itself. If the two names don't match, then the script will simply shutdown SheepShaver or Basilisk II after a few seconds.

I've tried using a variable for the application name, but that doesn't seem to work, at least not on 7.55.

Code: Select all

global appRunning
global cycles

on run
	tell application "APPLICATION NAME HERE" to activate
	set cycles to 1
end run

on idle
	tell application "Finder"
		tell application "Finder" to name of processes
		if the result contains "APPLICATION NAME HERE" then
			set appRunning to true
		else
			set appRunning to false
		end if
	end tell
	if appRunning is false then
		if cycles is greater than 1 then
			-- tell me to quit -- use this for testing to avoid shutdown
			tell application "Finder" to shut down
		end if
	end if
	set cycles to cycles + 1
	return 5 -- number of seconds between tests for application running
end idle
User avatar
Ronald P. Regensburg
Expert User
Posts: 7821
Joined: Thu Feb 09, 2006 10:24 pm
Location: Amsterdam, Netherlands

Post by Ronald P. Regensburg »

The usual way to this is to put an alias of the application in the Startup Items folder in the System Folder, no script needed. At startup the application will launch. And shutting down the emulated machine will of course also quit the application, seems to me just a simple as making the emulated machine shut down by quitting the application.
emendelson
Forum All-Star
Posts: 1706
Joined: Tue Oct 14, 2008 12:12 am

Post by emendelson »

Yes, I see - that is MUCH simpler. There is no real advantage to this script method except (maybe) this very small one: if you close the application first, you don't have to close down the system itself, because the system closes automatically. You can save exactly two mouse clicks by my method, so I probably spent a million times more effort writing the script that I can ever save by using my script...!
User avatar
Ronald P. Regensburg
Expert User
Posts: 7821
Joined: Thu Feb 09, 2006 10:24 pm
Location: Amsterdam, Netherlands

Post by Ronald P. Regensburg »

emendelson wrote:I probably spent a million times more effort writing the script that I can ever save by using my script...!
Well, it was a good exercise in writing AppleScript. :wink:
Gannet
Student Driver
Posts: 11
Joined: Sun Jan 24, 2010 2:00 am

Post by Gannet »

This is actually quite useful, thanks emendelson. Though for the application I was doing this with it seemed no delay was necessary (either on 7.6.1 or 9.0.4), so with an alias to the app in startup items it turned out all I needed was this:

Code: Select all

on idle
    tell application "Finder" to if (count of processes) is 1 then shut down
    return 2 -- the code is so much shorter I figure it won't hurt to run it more often
end idle
If you do need a delay though, you could try putting a repeat in the run handler to do something silly for a while.
emendelson
Forum All-Star
Posts: 1706
Joined: Tue Oct 14, 2008 12:12 am

Post by emendelson »

Gannet wrote:This is actually quite useful, thanks emendelson. Though for the application I was doing this with it seemed no delay was necessary (either on 7.6.1 or 9.0.4), so with an alias to the app in startup items it turned out all I needed was this:

Code: Select all

on idle
    tell application "Finder" to if (count of processes) is 1 then shut down
    return 2 -- the code is so much shorter I figure it won't hurt to run it more often
end idle
If you do need a delay though, you could try putting a repeat in the run handler to do something silly for a while.
Very elegant! Unfortunately, it produces an error message in 7.5.5 - "the result of a numeric operation was too large". I think AppleScript improved a lot between 7.5.5 and 7.6.1...
User avatar
classicmacreborn
Inquisitive Elf
Posts: 38
Joined: Tue Jan 24, 2017 6:58 am

Re: AppleScript to launch app, shut down when closing app

Post by classicmacreborn »

Thanks for writing this!

I was working basically the same script until I found this.

As you say, you can put something in startup items, but you have to shut down the OS to exit the emulator. Requires a script to get the OS to shut down when the application is exited.
User avatar
classicmacreborn
Inquisitive Elf
Posts: 38
Joined: Tue Jan 24, 2017 6:58 am

Re: AppleScript to launch app, shut down when closing app

Post by classicmacreborn »

Here is the solution:

Code: Select all

tell application "Finder"
	open file "Macintosh HD:ApplicationName"
	shut down
end tell
If the application is launched as a file with finder, the finder will issue the shut down command once that application is quit.
User avatar
adespoton
Forum All-Star
Posts: 4227
Joined: Fri Nov 27, 2009 5:11 am
Location: Emaculation.com
Contact:

Re: AppleScript to launch app, shut down when closing app

Post by adespoton »

We need this on the wiki somewhere; I keep on arriving at this solution every 4 years or so. Then I forget about it.
User avatar
classicmacreborn
Inquisitive Elf
Posts: 38
Joined: Tue Jan 24, 2017 6:58 am

Re: AppleScript to launch app, shut down when closing app

Post by classicmacreborn »

It's not a hugely reliable solution though. Sometimes shut down doesn't work for who knows what reason. It's much better to replace the Finder with the application. It also boots faster and prevents Finder or the user from messing with the OS and destabilizing the wrapped program. But that doesn't cause the OS to shut down after the application is quit so the user gets stuck.

I've been working on a Finder replacement in C++/MPW called "WrapperFinder" that actually issues direct commands to Toolbox to launch the application and then force a (usually clean) shutdown after the application has been quit. You should be able to specify the absolute path of the application in a Finder.cfg file in the System Folder- as using an alias in Startup Items is notoriously unreliable.

So far, it's fully functional when using ToolServer and an MPW StartupTS script as a Finder replacement. ToolServer has some lag and weirdnesses as it loads up all of the MPW interfaces before executing the script, and it can't be used as a library in an application (if anyone knows otherwise, you would save me so much time). But it still works WAY better than using an AppleScript. Right now I'm implementing the Apple Events manually in C++. Shut Down works, but the LaunchApplication API in Process Manager is taking some time to implement. LaunchApplication is a nightmare of hierarchical structs and pointers. It will definitely work though (if it works in MPW shell you can always implement it in C++).

Another thing that will help with reliability is to make the OS drive read-only and put the application in a separate drive. I haven't tried that yet, but I'll get there. I'm guessing OS9 will yell at me for it and it will break stuff.
User avatar
classicmacreborn
Inquisitive Elf
Posts: 38
Joined: Tue Jan 24, 2017 6:58 am

Re: AppleScript to launch app, shut down when closing app

Post by classicmacreborn »

adespoton wrote:We need this on the wiki somewhere; I keep on arriving at this solution every 4 years or so. Then I forget about it.
Actually, I'd love to add this to the wiki under the "misc" section if anyone is interested. I'll talk to the necessary parties whenever I change anything. But there are a definitely a number of things I could contribute if you're interested.

No worries if you can't though.
User avatar
adespoton
Forum All-Star
Posts: 4227
Joined: Fri Nov 27, 2009 5:11 am
Location: Emaculation.com
Contact:

Re: AppleScript to launch app, shut down when closing app

Post by adespoton »

I'm interested in the FinderWrapper application; I keep meaning to build one of those or find one I used to use in the System 6 days, but it would take me too long to get my toolchain set back up and my head in the right space. So I'm more than happy to use yours :)

Eventually, I'd like to create an app that you can drag a Classic application onto, and it'll automatically set it up to auto-boot/shut down in a wrapper app -- kind of like Boxer does for DOS software.
jrethorst
Tinkerer
Posts: 68
Joined: Sun Nov 05, 2006 1:21 am

Re: AppleScript to launch app, shut down when closing app

Post by jrethorst »

Nice custom solution that a lot of people have found useful. Thank you.

Where you say it's important to specify the application by exact name: you can get the name of an app on a given machine from its four-character file creator. For example, the creator of WP Mac is "WPC2". While a user can name an app anything, its file creator doesn't change. So:

Code: Select all

tell application "Finder" to get name of application file id "WPC2"
--> "WordPerfect" on my machine.

This works on OSX as well if the app in question has a creator type; not all do any more.

John R.
Post Reply