OSDN Git Service

upgrade to 3.6.2
[jnethack/source.git] / win / macosx / NetHackTerm.applescript
1 #!/usr/bin/osascript
2 # NetHack 3.6.2  NetHackTerm.applescript $NHDT-Date: 1524684597 2018/04/25 19:29:57 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.8 $
3 # Copyright (c) Kenneth Lorber, Kensington, Maryland, 2011
4 # NetHack may be freely redistributed.  See license for details.
5
6 # Run the terminal port from the GUI.
7
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
14
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
21         
22         activate
23         #open new window and run NetHack in it
24         do script with command "clear;sleep 1;/usr/local/bin/nethack;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
42         
43         # main loop - wait for all nethack processes to exit
44         set hit to true
45         set nhname to "nethack" as text
46         delay 4
47         repeat while hit
48                 set hit to false
49                 delay 0.5
50                 
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
57                 
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
76         
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
90         
91 end tell
92
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
111
112 if debug then
113         display alert "LATE COUNT is " & wc
114 end if
115 repeat with win in pending
116         try
117                 close win
118         end try
119 end repeat
120
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