E-Maculation
https://www.emaculation.com/forum/

AppleScript to launch app, shut down when closing app
https://www.emaculation.com/forum/viewtopic.php?f=20&t=6133
Page 1 of 1

Author:  emendelson [ Mon Jan 18, 2010 5:49 pm ]
Post subject:  AppleScript to launch app, shut down when closing app

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:
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

Author:  Ronald P. Regensburg [ Mon Jan 18, 2010 8:07 pm ]
Post subject: 

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.

Author:  emendelson [ Mon Jan 18, 2010 8:53 pm ]
Post subject: 

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...!

Author:  Ronald P. Regensburg [ Mon Jan 18, 2010 10:57 pm ]
Post subject: 

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:

Author:  Gannet [ Mon Jan 25, 2010 10:10 am ]
Post subject: 

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:
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.

Author:  emendelson [ Tue Jan 26, 2010 1:01 am ]
Post subject: 

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:
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...

Author:  classicmacreborn [ Tue Jan 24, 2017 11:44 am ]
Post subject:  Re: AppleScript to launch app, shut down when closing app

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.

Author:  classicmacreborn [ Sat Jan 05, 2019 9:17 pm ]
Post subject:  Re: AppleScript to launch app, shut down when closing app

Here is the solution:
Code:
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.

Author:  adespoton [ Sun Jan 06, 2019 4:47 am ]
Post subject:  Re: AppleScript to launch app, shut down when closing app

We need this on the wiki somewhere; I keep on arriving at this solution every 4 years or so. Then I forget about it.

Author:  classicmacreborn [ Sun Jan 06, 2019 11:12 pm ]
Post subject:  Re: AppleScript to launch app, shut down when closing app

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.

Author:  classicmacreborn [ Mon Jan 07, 2019 4:22 am ]
Post subject:  Re: AppleScript to launch app, shut down when closing app

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.

Author:  adespoton [ Mon Jan 07, 2019 5:49 pm ]
Post subject:  Re: AppleScript to launch app, shut down when closing app

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.

Author:  jrethorst [ Tue Feb 05, 2019 5:01 pm ]
Post subject:  Re: AppleScript to launch app, shut down when closing app

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:
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.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/