[Pythonmac-SIG] How-To type/creator and walking through direc tories?

Schollnick, Benjamin Benjamin.Schollnick@usa.xerox.com
Tue, 12 Mar 2002 07:59:07 -0500


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

--Boundary_(ID_f9HEGowPMe0C/DLIhEPURA)
Content-type: text/plain;	charset="utf-8"

Included in with the python distrib., is a script called "fixfiletypes",
it can and will do what your asking for..... Although, you may have to look
up the creator/type code(s).

The original script is designed to reset the file types for python scripts &
C files,
 but I've made a few modifications....(And included my changed version with
this message).

1) The original (at least python v2.0), is case sensitive to the file
extension.

fix:
if os.path.isfile(name):
		for ext, cr, tp in list:
			if string.upper(name[-len(ext):]) == ext:
				fs = macfs.FSSpec(name)
				curcrtp = fs.GetCreatorType()
				if curcrtp <> (cr, tp):
					if change:
						fs.SetCreatorType(cr, tp)
						macostools.touched(fs)
						print 'Fixed ', name
					else:
						print 'Wrong', curcrtp, name

	The string.upper(name..... removes this dependency....

2) It only contains creator, etc, information for python code....
	(I seem to remember that the original had a few additional entries,
	but I'm not motivated enough to verify this):

	The #1 change, impact the list now... The file extension has to
	be in uppercase, but you now only need a single entry....

	If anyone else would like to contribute additional Creator/type
codes
	please feel free to contact me...

	# File Extension
				# Creator Code
						# Type Code
list = [
	('.AIFF', 	'SCPL', 'AIFF'),
	('.AVI', 	'TVOD', 'VfW '),	# AVI Format (Quicktime)
	('.AS', 	'ToyS', 'TEXT'),
	('.C', 	'CWIE', 'TEXT'),
	('.CMC', 	'CMIF', 'CMC '),
	('.CMIF', 	'CMIF', 'TEXT'),
	('.GIF', 	'GKON', 'GIFf'),	# GIF Format (Graphic
Converter)
	('.H', 	'CWIE', 'TEXT'),
	('.HQX', 	'SITx', 'TEXT'),	# HQX (Stuffit Archive)
	('.JPG', 	'GKON', 'JPEG'),	# JPG (Graphic Converter)
	('.JPEG', 	'GKON', 'JPEG'),	# JPG (Graphic Converter)
	('.MOV', 	'TVOD', 'MooV'),	# Quicktime (MOV)
	('.MOOV', 	'TVOD', 'MooV'),	# Quicktime (MOV)
	('.MPG', 	'TVOD', 'MPG '),	# Quicktime (MPG)
	('.MPEG', 	'TVOD', 'MPG '),	# Quicktime (MPG)
	('.PDF', 	'CARO', 'PDF '),	# Adobe Acrobat PDF File
	('.PM6', 	'ALD6', 'ALB6'),	# Pagemaker v6.0 File
	('.P65', 	'AD65', 'APPL'),	
	('.PY', 	'Pyth', 'TEXT'),
	('.PYC', 	'Pyth', 'PYC '),
	('.SMI', 	'oneb', 'APPL'),
	('.WAV', 	'TVOD', 'WAVE'),
	('.XLS', 	'XCEL', 'XLS8'),
	('.ZIP', 	'SITx', 'ZIP ')
]

I've found this script to be extremely helpful, and powerful.

Of course, "with great power, comes great responsiblity".
Misuse of this script, can impede or even "damage" your system.
A bad type/creator code, can make files unusable until you reset the
code(s) to the proper settings.

			- Benjamin
-----Original Message-----
From: macnerd [mailto:macnerd@realmspace.com]
Sent: Monday, March 11, 2002 1:36 PM
To: pythonmac-SIG@python.org
Subject: [Pythonmac-SIG] How-To type/creator and walking through
directories?


I have a major annoyance in that I have a CD of HTMLs and GIFs, which have
generic TYPE/CREATORS.
I would like to change this to TYPE/CREATOR of my choosing, so I want to
create a tool that will walk
through all of the directories.

I'm so new to the Mac scripting, so I was wondering if someone could tell me
how can I change the 
type/creator and walk through the folders.  Do the folders look like UNIX
directories in MacPython?
Are there symbolic . and ..?

_______________________________________________
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig


--Boundary_(ID_f9HEGowPMe0C/DLIhEPURA)
Content-type: application/octet-stream;	name="fixfiletypes.py"
Content-disposition: attachment;	filename="fixfiletypes.py"

#
# 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
import macostools
import string

	# File Extension
				# Creator Code
						# Type Code
list = [
	('.AIFF', 	'SCPL', 'AIFF'),
	('.AVI', 	'TVOD', 'VfW '),	# AVI Format (Quicktime)
	('.AS', 	'ToyS', 'TEXT'),
	('.C', 		'CWIE', 'TEXT'),
	('.CMC', 	'CMIF', 'CMC '),
	('.CMIF', 	'CMIF', 'TEXT'),
	('.GIF', 	'GKON', 'GIFf'),	# GIF Format (Graphic Converter)
	('.H', 		'CWIE', 'TEXT'),
	('.HQX', 	'SITx', 'TEXT'),	# HQX (Stuffit Archive)
	('.JPG', 	'GKON', 'JPEG'),	# JPG (Graphic Converter)
	('.JPEG', 	'GKON', 'JPEG'),	# JPG (Graphic Converter)
	('.MOV', 	'TVOD', 'MooV'),	# Quicktime (MOV)
	('.MOOV', 	'TVOD', 'MooV'),	# Quicktime (MOV)
	('.MPG', 	'TVOD', 'MPG '),	# Quicktime (MPG)
	('.MPEG', 	'TVOD', 'MPG '),	# Quicktime (MPG)
	('.PDF', 	'CARO', 'PDF '),	# Adobe Acrobat PDF File
	('.PM6', 	'ALD6', 'ALB6'),	# Pagemaker v6.0 File
	('.P65', 	'AD65', 'APPL'),	
	('.PY', 	'Pyth', 'TEXT'),
	('.PYC', 	'Pyth', 'PYC '),
	('.SMI', 	'oneb', 'APPL'),
	('.WAV', 	'TVOD', 'WAVE'),
	('.XLS', 	'XCEL', 'XLS8'),
	('.ZIP', 	'SITx', 'ZIP ')
]

def walktree(name, change):
	if os.path.isfile(name):
		for ext, cr, tp in list:
			if string.upper(name[-len(ext):]) == ext:
				fs = macfs.FSSpec(name)
				curcrtp = fs.GetCreatorType()
				if curcrtp <> (cr, tp):
					if change:
						fs.SetCreatorType(cr, tp)
						macostools.touched(fs)
						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):
	fss, ok = macfs.GetDirectory('Folder to search:')
	if not ok:
		sys.exit(0)
	walktree(fss.as_pathname(), change)
	
if __name__ == '__main__':
	run(1)
	
	

--Boundary_(ID_f9HEGowPMe0C/DLIhEPURA)--