2 # aNetHack
0.0.1 aNetHackTerm.applescript $ANH-Date$ $ANH-Branch$:$ANH-Revision$
3 #
Copyright (c) Kenneth Lorber, Kensington, Maryland,
2011
4 # aNetHack may be freely redistributed. See license
for details.
6 # Run
the terminal port
from the GUI.
9 # We
close any terminal
windows with no processes
in them, even
if they
10 # don't belong
to us because we can't really
tell which ones do belong
to us.
11 # Shouldn't be serious since anyone using this
is unlikely
to be using Terminal
15 set needshutdown
to false
16 tell application "Terminal"
17 # see
if we're going
to have
to shut it down
at the end because we started it up
18 if it
is not running
then
19 set needshutdown
to true
23 #
open new window and run aNetHack
in it
24 do
script with command
"clear;sleep 1;/usr/local/bin/anethack;echo '(press RETURN to exit)';awk '{exit}';exit"
25 set nhresult
to result
-- class is tab
26 set nhresrec
to result
as record
27 set nhreslist
to result
as list
28 set nhwin
to item 4 of nhreslist
29 set custom
title of nhwin
to "NH"
30 #
set title displays custom
title of nhresult
to true
31 set title displays device
name of nhresult
to false
32 set title displays shell
path of nhresult
to false
33 set title displays
window size of nhresult
to false
34 set title displays
file name of nhresult
to false
35 #
set idnum
to id
of nhresult
36 set xxx
to class of nhresrec
37 get class of nhresrec
-- record
38 get length of nhresrec
-- 4
39 set nhwinname
to name of nhwin
40 set nhtab
to selected tab
of nhwin
41 get processes
of nhtab
43 # main loop - wait
for all anethack processes
to exit
45 set nhname
to "anethack" as text
51 # don't blow up
if the window has gone away
53 set procs
to get processes
of nhtab
58 repeat with pname
in procs
60 display alert
"PNAME=" & pname &
"=" & nhname &
"="
62 # XXX this test
is odd, but more obvious tests fail
63 if pname starts
with nhname
then
64 #
display alert
"HIT" -- dangerous - infinite loop
68 if pname starts
with (
"awk" as text)
then
77 #
window may have already closed
or dropped its
last process (
error -
1728)
79 close window nhwinname
80 on error errText
number errNum
81 if errNum = -
1728 then
83 display alert
"close failed (expected)"
86 -- unexpected error - show it
87 display alert
"close failed: " & errText &
" errnum=" & errNum
93 # Close all
the windows that don't have any live processes
in them.
94 tell application "Terminal"
95 set wc
to count windows
98 display alert
"COUNT is " & wc
100 repeat with win
in windows
102 display alert
"WIN: " & (
name of win) &
" TABS: " & (
count of tabs
of win) &
" PROCS: " & (
count of processes
of item 1 of tabs
of win)
104 set pcount
to count of processes
of item 1 of tabs
of win
106 copy win
to the end of pending
113 display alert
"LATE COUNT is " & wc
115 repeat with win
in pending
121 # If we started Terminal.app
and the user doesn't have anything
else running,
123 if needshutdown
and (wc
is 0)
then
125 display alert
"trying shutdown"
127 tell application "Terminal"