fixfiletypes.py 1.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#
# Fixfiletypes - Set mac filetypes to something sensible,
#  recursively down a directory tree.
#
# It will only touch extensions it feels pretty sure about.
# This script is useful after copying files from unix.
#
# Jack Jansen, CWI, 1995.
#
import os
import macfs
import sys
13
import macostools
14 15

list = [
16 17
	('.py', 'Pyth', 'TEXT'),
	('.pyc', 'Pyth', 'PYC '),
18 19 20
	('.c', 'CWIE', 'TEXT'),
	('.h', 'CWIE', 'TEXT'),
	('.as', 'ToyS', 'TEXT'),
21 22
	('.hqx', 'BnHq', 'TEXT'),
	('.cmif', 'CMIF', 'TEXT'),
Jack Jansen's avatar
Jack Jansen committed
23 24 25
	('.cmc', 'CMIF', 'CMC '),
	('.aiff', 'SCPL', 'AIFF'),
	('.mpg', 'mMPG', 'MPEG'),
26 27 28 29 30 31 32 33 34 35 36
]

def walktree(name, change):
	if os.path.isfile(name):
		for ext, cr, tp in list:
			if name[-len(ext):] == ext:
				fs = macfs.FSSpec(name)
				curcrtp = fs.GetCreatorType()
				if curcrtp <> (cr, tp):
					if change:
						fs.SetCreatorType(cr, tp)
37
						macostools.touched(fs)
38 39 40 41 42 43 44 45 46 47
						print 'Fixed ', name
					else:
						print 'Wrong', curcrtp, name
	elif os.path.isdir(name):
		print '->', name
		files = os.listdir(name)
		for f in files:
			walktree(os.path.join(name, f), change)
			
def run(change):
48
	fss, ok = macfs.GetDirectory('Folder to search:')
49 50 51 52 53 54 55 56
	if not ok:
		sys.exit(0)
	walktree(fss.as_pathname(), change)
	
if __name__ == '__main__':
	run(1)