NHDT->ANH, nethack->anethack, nhdat->anhdat
[aNetHack.git] / win / macosx / NetHackTerm.applescript
blobb0ef35b9c84ce2e5a85f5dc3a77fe243ed128374
1 #!/usr/bin/osascript
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.
8 # BUGS:
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
12 # for anything else.
13 set debug to false
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
20 end if
22 activate
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
44 set hit to true
45 set nhname to "anethack" as text
46 delay 4
47 repeat while hit
48 set hit to false
49 delay 0.5
51 # don't blow up if the window has gone away
52 try
53 set procs to get processes of nhtab
54 on error number -1728
55 exit repeat
56 end try
58 repeat with pname in procs
59 if debug then
60 display alert "PNAME=" & pname & "=" & nhname & "="
61 end if
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
65 set hit to true
66 end if
67 # yes, this is scary.
68 if pname starts with ("awk" as text) then
69 set hit to true
70 end if
71 end repeat
72 end repeat
73 if debug then
74 display alert "DONE"
75 end if
77 # window may have already closed or dropped its last process (error -1728)
78 try
79 close window nhwinname
80 on error errText number errNum
81 if errNum = -1728 then
82 if debug then
83 display alert "close failed (expected)"
84 end if
85 else
86 -- unexpected error - show it
87 display alert "close failed: " & errText & " errnum=" & errNum
88 end if
89 end try
91 end tell
93 # Close all the windows that don't have any live processes in them.
94 tell application "Terminal"
95 set wc to count windows
96 set pending to {}
97 if debug then
98 display alert "COUNT is " & wc
99 end if
100 repeat with win in windows
101 if debug then
102 display alert "WIN: " & (name of win) & " TABS: " & (count of tabs of win) & " PROCS: " & (count of processes of item 1 of tabs of win)
103 end if
104 set pcount to count of processes of item 1 of tabs of win
105 if pcount is 0 then
106 copy win to the end of pending
107 set wc to wc - 1
108 end if
109 end repeat
110 end tell
112 if debug then
113 display alert "LATE COUNT is " & wc
114 end if
115 repeat with win in pending
117 close win
118 end try
119 end repeat
121 # If we started Terminal.app and the user doesn't have anything else running,
122 # shut it down.
123 if needshutdown and (wc is 0) then
124 if debug then
125 display alert "trying shutdown"
126 end if
127 tell application "Terminal"
128 quit
129 end tell
130 end if