77"""
88
99import sys
10+ sys .stdout = sys .stderr
11+
1012import string
1113import os
1214import marshal
@@ -38,21 +40,35 @@ def main():
3840 template = os .path .join (p , TEMPLATE )
3941 try :
4042 tmpl = open (template , "rb" )
43+ tmpl .close ()
4144 break
4245 except IOError :
4346 continue
4447 else :
4548 # XXX Ought to put up a dialog
46- print "Template not found. Exit. "
49+ print "Template" , `template` , " not found"
4750 return
4851
49- # Ask for source text
52+ # Ask for source text if not specified in sys.argv[1:]
5053
51- srcfss , ok = macfs .StandardGetFile ('TEXT' )
52- if not ok :
53- tmpl .close ()
54- return
55- filename = srcfss .as_pathname ()
54+ if not sys .argv [1 :]:
55+ srcfss , ok = macfs .StandardGetFile ('TEXT' )
56+ if not ok :
57+ return
58+ filename = srcfss .as_pathname ()
59+ if not sys .argv : sys .argv .append ('' )
60+ sys .argv .append (filename )
61+
62+ # Loop over all files to be processed
63+
64+ for filename in sys .argv [1 :]:
65+ process (template , filename )
66+
67+ undefs = ('????' , ' ' , '\0 \0 \0 \0 ' )
68+
69+ def process (template , filename ):
70+
71+ print "Processing" , `filename` , "..."
5672
5773 # Read the source and compile it
5874 # (there's no point overwriting the destination if it has a syntax error)
@@ -63,8 +79,7 @@ def main():
6379 try :
6480 code = compile (text , filename , "exec" )
6581 except (SyntaxError , EOFError ):
66- print "Syntax error in script"
67- tmpl .close ()
82+ print "Syntax error in script" , `filename`
6883 return
6984
7085 # Set the destination file name
@@ -76,6 +91,7 @@ def main():
7691
7792 # Copy the data from the template (creating the file as well)
7893
94+ tmpl = open (template , "rb" )
7995 dest = open (destname , "wb" )
8096 data = tmpl .read ()
8197 if data :
@@ -84,8 +100,12 @@ def main():
84100 tmpl .close ()
85101
86102 # Copy the creator and type of the template to the destination
103+ # unless it already has one
87104
88- ctor , type = MacOS .GetCreatorAndType (template )
105+ tctor , ttype = MacOS .GetCreatorAndType (template )
106+ ctor , type = MacOS .GetCreatorAndType (destname )
107+ if type in undefs : type = ttype
108+ if ctor in undefs : ctor = tctor
89109 MacOS .SetCreatorAndType (destname , ctor , type )
90110
91111 # Open the input and output resource forks
@@ -161,6 +181,9 @@ def main():
161181 # Close the resource file
162182
163183 CloseResFile (output )
184+
185+ print "Done with" , `filename` , "..."
186+
164187
165188if __name__ == '__main__' :
166189 main ()
0 commit comments