This page is a wiki. Please login or create an account to begin editing.


7 posts / 0 new
Last post
SwedeBear's picture
Offline
Joined: 2009 Apr 18
AppleScript Help

Inspired by another recently revived thread here about SS wrapper I dug out my collection self-contained Classic Mac Games in order to try make a working 'Exit Game' with AS.
The systems are US versions either 7.6.1 or 8.6 w appropriate version of QT and using 'Virtual DVD-ROM/CD Utility' w associated Toast image file/s.

I can mount image, start game application and quit Finder on system startup w AS. But I can't manage to make the script idle and when game application is quitted the script ejects the disk image and starts app '• Shut Down' which does what it says.

The script looks like:

tell application "Finder"
activate
select file "Myst.toast" of disk "GameSpace"
open selection
select file " Myst" of disk "GameSpace"
open selection
application "Finder"
quit
end tell
on idle
if application " Myst" exists then
else
tell application "Finder"
activate
eject disk "Myst"
select file "• Shut Down" of disk "GameSpace"
open selection
end tell
end if
end idle

I'm by no means fluent in AS so this is cut & paste trials. Any thoughts/corrections is gladly accepted

Comments

MikeTomTom's picture
Offline
Joined: 2009 Dec 7

Its a long time since I've looked at classic AppleScripting...

Use:
put away disk "Myst" instead of eject disk (cosmetic, eject command leaves a dimmed icon behind on the desktop, "put away" ejects the disk cleanly).

Use processes instead of exists -- "processes" are the running apps you see listed under the top right-hand Finder menu - e.g.;

set x to (every process whose file type is not "FNDR") as text
set y to application " Myst" -- you may be asked to locate this (one time only).
if y is in x then
    do something else -- such as run your idle handler
end if

If you use the command "shut down" at the end of your script instead of select file "• Shut Down" of disk "GameSpace" -- it should do the same task without needing the external shutdown program.

The idle bit is more difficult and I think it should be called into use from an if, else type handler.
Idle in AppleScript runs every 30 seconds by default. You can change this by setting it to run every "n" seconds, e.g.;

on idle
    beep
    return 5
end idle

MikeTomTom's picture
Offline
Joined: 2009 Dec 7

...I can't manage to make the script idle

From what I can tell, idle scripts in classic AppleScript only run in the "stay open" application form.

So if you copy and paste from between the dotted lines below into a new AppleScript:

------------------------------------
property idlePeriod : 5

tell application "Finder"
    select file "Myst.toast" of disk "GameSpace"
    open selection
    select file " Myst" of disk "GameSpace"
    open selection
end tell

on idle
    tell application "Finder"
       set x to every process
       set y to application " Myst"
       if y is in x then
          return idlePeriod
       else -- Myst has quit
          put away disk "Myst"
          shut down
       end if
    end tell
end idle
------------------------------------

Save the script and give it some name, - I'd named my test script app, "Play Mysty for me".
Choose Kind: Application and check both "Stay Open" and "Never Show Startup Screen".
The above works for me and parses the idle handlers correctly.

I set the idle time to 5 seconds (property idlePeriod : 5), you can change this to suit. A lower number would quicken the shutdown process once you click "Quit" in Myst.

Before placing an alias of this app into your Startup folder, run it, as it may ask where the application " Myst" is located, which it will do only the one time, if at all (or if you move Myst to a different named drive, etc). Note: the game application Myst has a space in front of it's name by default, so you need to include the space in the script and quoted as " Myst".

The line in the above script; put away disk "Myst", is probably redundant because the Finder's command to "shut down" will do this anyway (if the disk was a real CD it could be more useful to use). The external shutdown you used in your script is also redundant. There's no need to ask the Finder to "activate" here either.

If I was to use this I would more likely be inclined to have it open a "Myst Saved Game" instead of the main application, just to get passed the intro at start up. I would also give it some shutdown options such as rebooting and quitting to the desktop if I wanted. But if all you need is to launch into a game and quit the emulator at the end, then the above will do that.

SwedeBear's picture
Offline
Joined: 2009 Apr 18

Thanks MTT! It worked almost directly Wink
I got the MYST app complaining 'Not Finding Resources' ie as if the CD wasn't mounted and then it shutdown leaving the Idle function confused, naturally.
By trial & error I got your script fully working by adding 'activate' after the 1st line in the 'Tell' statement like this:

property idlePeriod : 0.5

tell application "Finder"
activate
select file "Myst.toast" of disk "GameSpace"
open selection
select file " Myst" of disk "GameSpace"
open selection
end tell

on idle
tell application "Finder"
set x to every process
set y to application " Myst"
if y is in x then
return idlePeriod
else -- Myst has quit
put away disk "Myst"
shut down
end if
end tell
end idle

Don't ask me why it works, maybe it brings up Finder and keeps it on top thus blocking the MYST application initialize?
I'll consider a Startup meny with choices running afresh or open a saved game but 1st go through the bunch of game packages I have here. I can up this MYST package if someone wants to have a go at it. The System folder can probably be stripped down considerably. Many thanks again MTT!

MikeTomTom's picture
Offline
Joined: 2009 Dec 7

YW - it was fun, as I hadn't looked at AppleScripting for a long time.

Telling Finder to activate wasn't required for me, perhaps your "property idlePeriod : 0.5" is too short a time frame and idle begins before the image has time to fully mount. At 0.5, idle would kick in at almost the same time as the open selection command was sent to launch " Myst".

I would use a longer idle period; - it took my script set at 5 seconds idle, approx. 3 seconds for Myst to fully quit + another 2 seconds to shut the emulator down.
Try idlePeriod : 3 (or 2 or 4, etc).

The script is bare bones and could do with additional "try" handlers to avoid potential errors.

Something you could also test, is to comment out the lines:
-- select file "Myst.toast" of disk "GameSpace"
-- open selection

Instead, place an alias to disk image Myst.toast into the Startup Items folder, name it so it launches to mount before the AppleScript app loads. e.g.; "a Myst.toast"

You could easily comment out or remove -- put away disk "Myst". The command to "shut down" handles this. Ejecting the disk would only be desirable if the disk was an actual CD/DVD. - It doesn't hurt to have the put away handler there, but its not required for unmounting disk images when shutting down the emulation.

Best if the game isn't upped to here tho', thanks. There's a policy of not accepting compiled wrapped games, which this would likely fall under.

SwedeBear's picture
Offline
Joined: 2009 Apr 18

Well, it certainly cranks some very unused cogs and chains at my place. Today's mostly CSS/HTML etc. Wink
The complaint from 'Myst' stayed with different values on 'IdlePeriod' and disappeared with the addition of 'activate'. My 'IdlePeriod' settings is just an estimation as I'm still dumped in 'Finder' for a couple of moments after quitting 'Myst' before 'IdlePeriod' is over. Are you running this on the real thing btw?
Also if I were to quit 'Finder' at start could I use 'processes' without activate 'Finder' again? It would make a more 'complete' impression when shutting down.
Thanks for the headup on wrapped games here. I've missed that one somehow.

MikeTomTom's picture
Offline
Joined: 2009 Dec 7

Also if I were to quit 'Finder' at start could I use 'processes' without activate 'Finder' again?

No, processes are a "Process object class" belonging to the Finder, if Finder was to quit then the idle commands wouldn't know what to do.

Also, "shut down" is a Finder command.

You could perhaps go back to using the "select file "• Shut Down" of disk "GameSpace"
open selection" handler, if this makes shutting down look less like you're being thrown back onto the desktop once Myst has quit from the processes.

Important: If you opt for this, you should also add a "quit" handler for the AppleScript app as a safety measure, to prevent the script from going into an infinite idle loop or error:
--------------------------------
on idle
   tell application "Finder"
      set x to every process
      set y to application " Myst"
      if y is in x then
         return idlePeriod
      else -- Myst has quit
         select file "• Shut Down" of disk "GameSpace"
         open selection
         quit me -- literal, exits idle, & goes to "on quit" handler
      end if
   end tell
end idle

on quit
    continue quit -- required for quitting "stay open" AS applications
end quit
--------------------------------

Are you running this on the real thing btw?

I haven't been running this on the real thing, and I could be, lazy me. No, I used Mini vMac (Mac II, variation build) to write and test the thing. - all in 8MB total RAM Smile

Myst runs OK in it but sound is an issue with Mini vMac, which makes it not very good for playing games where sound is important.

I've just tried it out in SheepShaver (Mac OS 7.6.1) on Windows, I don't get the issue with needing Finder to activate, but I was asked where "Myst" was and I also needed to "Save As..." to get the location of Myst to stick... This occurred when I opened the script app with the Script Editor to review the AppleScript coding.

Perhaps the differences here could be down to platform, Mac OS version, or even version of SheepShaver. Myst however requires 256 colors and asks beforehand to switch screen resolution, which causes the script to fail, as idle kicks in before Myst is in the processes. The solution to this was to switch to 256 colors before launching the script app (and to run the emulation in 256 colors only from then on).

One thing obvious to me when testing in SheepShaver (which I run full screen, not windowed) was at quit time. It was a lot uglier to look at than when testing with Mini vMac with lots of discolored pixelation going on. It would be a whole lot nicer to get a black screen before quitting the emulator.