from folder get list of files with attributes into textedit & excel

From a chosen folder get the list of contained files with their attributes and format into textedit & excel.
This applescript works. It ran on OsX 10.6 and Excel 2004.
Here is the code:

###############################
–Program: make file directory (of a folder) including attributes.
###############################
–This program asks for a folder selection.
–It then gets a list of the contained files (but not the subfolders)
–and their properties.
–It places this list into textedit and excel (as tab separated fields and CR separated records).
–It first removes any pre-existing CR’s (replacing them with Vertical tabs).
################################################MAIN

tell application “System Events”
set theFolder to choose folder

set k to 0
set theText to ""
set delim1 to tab

#============
#============================================labels

#===index
set indexgroup to ""
set indexgroup to "thisindex0" & delim1
#===dates
set dategroup to ""
#-
set dategroup to dategroup & "AttributeChangeDate1" & delim1
#-
set dategroup to dategroup & "LastOpen2" & delim1
#-
set dategroup to dategroup & "LastUsedDate1" & delim1
set dategroup to dategroup & "UsedDates1" & delim1
#-
set dategroup to dategroup & "FSContentChangeDate1" & delim1
#-
set dategroup to dategroup & "modificationdate3" & delim1
set dategroup to dategroup & "ContentModificationDate1" & delim1
set dategroup to dategroup & "FSCreationDate1" & delim1
#-
set dategroup to dategroup & "creationdate3" & delim1
set dategroup to dategroup & "ContentCreationDate1" & delim1

#===file
set filegroup to ""
set filegroup to filegroup & "DisplayName1" & delim1
set filegroup to filegroup & "displayedname3" & delim1
set filegroup to filegroup & "FSName1" & delim1

#set filegroup to filegroup & "fileslist0" & delim1
#set filegroup to filegroup & "fileslist3" & delim1

#===path
set pathgroup to ""
set pathgroup to pathgroup & "TextPosixFile0" & delim1
set pathgroup to pathgroup & "Path1" & delim1
set pathgroup to pathgroup & "TextClassicmacFile0" & delim1
set pathgroup to pathgroup & "URL3" & delim1

#===kind
set kindgroup to ""
set kindgroup to kindgroup & "nameextension3" & delim1
set kindgroup to kindgroup & "kind1" & delim1
set kindgroup to kindgroup & "kind3" & delim1
set kindgroup to kindgroup & "class3" & delim1
set kindgroup to kindgroup & "ContentType1" & delim1

#===size
set sizegroup to ""
set sizegroup to sizegroup & "size3" & delim1
set sizegroup to sizegroup & "FSSize1" & delim1
set sizegroup to sizegroup & "physicalsize3" & delim1

#===other
set othergroup to ""
set othergroup to othergroup & "FinderComment1" & delim1
set othergroup to othergroup & "FSLabel1" & delim1
#===null
set nullgroup to ""
set nullgroup to nullgroup & "description1" & delim1
set nullgroup to nullgroup & "Creator1" & delim1
set nullgroup to nullgroup & "EncodingApplications1" & delim1
set nullgroup to nullgroup & "WhereFroms1" & delim1
set nullgroup to nullgroup & "SecurityMethod1" & delim1
set nullgroup to nullgroup & "Headline1" & delim1
set nullgroup to nullgroup & "title1" & delim1
set nullgroup to nullgroup & "Instructions1" & delim1
set nullgroup to nullgroup & "Keywords1" & delim1
set nullgroup to nullgroup & "Comment1" & delim1
set nullgroup to nullgroup & "TextContent1" & delim1
set nullgroup to nullgroup & "FSNodeCount1" & delim1
#===
set theText to theText & indexgroup
set theText to theText & dategroup
set theText to theText & filegroup
set theText to theText & pathgroup
set theText to theText & kindgroup
set theText to theText & sizegroup
set theText to theText & othergroup
set theText to theText & nullgroup
###############################
#============================================place end of line  record	
set theText to theText & "$"
#============================================

#=============

repeat with fileslist0 in (get files of theFolder)
	#-
	set k to k + 1
	set thisindex0 to (k as text)
	####################################################################
	#--------------------------------------------the common finder & system event group starts:	
	#---next does not work: can't make classicmac format file .DS_Store into type text
	#set fileslist3 to (fileslist0 as text)
	#-	
	set displayedname3 to (displayed name of fileslist0)
	set nameextension3 to (name extension of fileslist0)
	#-	
	set URL3 to (URL of fileslist0)
	#-	
	set size3 to (size of fileslist0)
	set physicalsize3 to (physical size of fileslist0)
	#-		
	set creationdate3 to (creation date of fileslist0)
	set modificationdate3 to (modification date of fileslist0)
	#-
	set class3 to (class of fileslist0)
	set kind3 to (kind of fileslist0)
	#--------------------------------------------the common finder & system event group ends:	
	####################################################################
	
	
	########################################################### Getting File Names			
	set pfileslist0 to POSIX path of fileslist0
	set TextPosixFile0 to (pfileslist0 as text)
	#-------------------------------------------use qpfileslist0 for the shell scripts----------
	set qpfileslist0 to quoted form of pfileslist0
	set qTextPosixFile0 to quoted form of TextPosixFile0
	#-------------
	set classicmacfile0 to (pfileslist0 as POSIX file)
	set TextClassicmacFile0 to (classicmacfile0 as text)
	#-------------
	#--- the next does not work: can't make quoted form of classicmac format of file .DS_Store into type reference
	#set qclassicmacfile0 to quoted form of classicmacfile0
	set qTextClassicmacFile0 to quoted form of TextClassicmacFile0
	#-------------			
	
	##########################################################Getting File Properties
	set LastOpen2 to do shell script "date -r $(stat -f %a " & qpfileslist0 & ") +%Y-%m-%dT%H:%M:%S"
	#==============	get data
	set ContentCreationDate1 to (do shell script "mdls -name kMDItemContentCreationDate -raw " & qpfileslist0)
	set ContentModificationDate1 to (do shell script "mdls -name kMDItemContentModificationDate -raw " & qpfileslist0)
	set AttributeChangeDate1 to (do shell script "mdls -name kMDItemAttributeChangeDate -raw " & qpfileslist0)
	set LastUsedDate1 to (do shell script "mdls -name kMDItemLastUsedDate -raw " & qpfileslist0)
	set UsedDates1 to (do shell script "mdls -name kMDItemUsedDates  -raw " & qpfileslist0)
	set FSCreationDate1 to (do shell script "mdls -name kMDItemFSCreationDate -raw " & qpfileslist0)
	set FSContentChangeDate1 to (do shell script "mdls -name kMDItemFSContentChangeDate -raw " & qpfileslist0)
	set kind1 to (do shell script "mdls -name kMDItemKind -raw " & qpfileslist0)
	set description1 to (do shell script "mdls -name kMDItemDescription -raw " & qpfileslist0)
	set ContentType1 to (do shell script "mdls -name kMDItemContentType -raw " & qpfileslist0)
	set Creator1 to (do shell script "mdls -name kMDItemCreator -raw " & qpfileslist0)
	set EncodingApplications1 to (do shell script "mdls -name kMDItemEncodingApplications -raw " & qpfileslist0)
	set WhereFroms1 to (do shell script "mdls -name kMDItemWhereFroms -raw " & qpfileslist0)
	set SecurityMethod1 to (do shell script "mdls -name kMDItemSecurityMethod -raw " & qpfileslist0)
	set DisplayName1 to (do shell script "mdls -name kMDItemDisplayName -raw " & qpfileslist0)
	set Headline1 to (do shell script "mdls -name kMDItemHeadline -raw " & qpfileslist0)
	set title1 to (do shell script "mdls -name kMDItemTitle -raw " & qpfileslist0)
	set Instructions1 to (do shell script "mdls -name kMDItemInstructions -raw " & qpfileslist0)
	set FSName1 to (do shell script "mdls -name kMDItemFSName -raw " & qpfileslist0)
	#---sometimes the next does not work
	set Path1 to (do shell script "mdls -name kMDItemPath -raw " & qpfileslist0)
	set FinderComment1 to (do shell script "mdls -name kMDItemFinderComment -raw " & qpfileslist0)
	set FSLabel1 to (do shell script "mdls -name kMDItemFSLabel -raw " & qpfileslist0)
	set FSSize1 to (do shell script "mdls -name kMDItemFSSize -raw " & qpfileslist0)
	set Keywords1 to (do shell script "mdls -name kMDItemKeywords -raw " & qpfileslist0)
	set Comment1 to (do shell script "mdls -name kMDItemComment -raw " & qpfileslist0)
	set TextContent1 to (do shell script "mdls -name kMDItemTextContent -raw " & qpfileslist0)
	set FSNodeCount1 to (do shell script "mdls -name kMDItemFSNodeCount -raw " & qpfileslist0)
	####################################################################
	
	
	#============================================ assign data
	#===index
	set indexgroup to ""
	set indexgroup to thisindex0 & delim1
	#===dates
	set dategroup to ""
	#-
	set dategroup to dategroup & AttributeChangeDate1 & delim1
	#-
	set dategroup to dategroup & LastOpen2 & delim1
	#-
	set dategroup to dategroup & LastUsedDate1 & delim1
	set dategroup to dategroup & UsedDates1 & delim1
	#-
	set dategroup to dategroup & FSContentChangeDate1 & delim1
	#-
	set dategroup to dategroup & modificationdate3 & delim1
	set dategroup to dategroup & ContentModificationDate1 & delim1
	set dategroup to dategroup & FSCreationDate1 & delim1
	#-
	set dategroup to dategroup & creationdate3 & delim1
	set dategroup to dategroup & ContentCreationDate1 & delim1
	#===file
	set filegroup to ""
	set filegroup to filegroup & DisplayName1 & delim1
	set filegroup to filegroup & displayedname3 & delim1
	set filegroup to filegroup & FSName1 & delim1
	
	#set filegroup to filegroup & fileslist0 & delim1
	#set filegroup to filegroup & fileslist3 & delim1
	
	#===path
	set pathgroup to ""
	set pathgroup to pathgroup & TextPosixFile0 & delim1
	set pathgroup to pathgroup & Path1 & delim1
	set pathgroup to pathgroup & TextClassicmacFile0 & delim1
	set pathgroup to pathgroup & URL3 & delim1
	#===kind
	set kindgroup to ""
	set kindgroup to kindgroup & nameextension3 & delim1
	set kindgroup to kindgroup & kind1 & delim1
	set kindgroup to kindgroup & kind3 & delim1
	set kindgroup to kindgroup & class3 & delim1
	set kindgroup to kindgroup & ContentType1 & delim1
	#===size
	set sizegroup to ""
	set sizegroup to sizegroup & size3 & delim1
	set sizegroup to sizegroup & FSSize1 & delim1
	set sizegroup to sizegroup & physicalsize3 & delim1
	#===other
	set othergroup to ""
	set othergroup to othergroup & FinderComment1 & delim1
	set othergroup to othergroup & FSLabel1 & delim1
	#===null
	set nullgroup to ""
	set nullgroup to nullgroup & description1 & delim1
	set nullgroup to nullgroup & Creator1 & delim1
	set nullgroup to nullgroup & EncodingApplications1 & delim1
	set nullgroup to nullgroup & WhereFroms1 & delim1
	set nullgroup to nullgroup & SecurityMethod1 & delim1
	set nullgroup to nullgroup & Headline1 & delim1
	set nullgroup to nullgroup & title1 & delim1
	set nullgroup to nullgroup & Instructions1 & delim1
	set nullgroup to nullgroup & Keywords1 & delim1
	set nullgroup to nullgroup & Comment1 & delim1
	set nullgroup to nullgroup & TextContent1 & delim1
	set nullgroup to nullgroup & FSNodeCount1 & delim1
	#===
	set theText to theText & indexgroup
	set theText to theText & dategroup
	set theText to theText & filegroup
	set theText to theText & pathgroup
	set theText to theText & kindgroup
	set theText to theText & sizegroup
	set theText to theText & othergroup
	set theText to theText & nullgroup
	
	###############################
	#============================================place end of line  record	
	set theText to theText & "$"
	#============================================	
end repeat

end tell

#########subroutine that replaces a substring
on replace_chars(this_text, search_string, replacement_string)
set AppleScript’s text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript’s text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript’s text item delimiters to “”
return this_text
end replace_chars
############## Replace CR with Vtab {ascii(11 (0B hexad))}. Replace $ with CR {ascii(13 (0d hexad))}.
#—
set cr1 to return
set dollar1 to “$”
set vtab1 to {ASCII character 11}

set theText to replace_chars(theText, cr1, vtab1)
set theText to replace_chars(theText, dollar1, cr1)
#—

####################################################################Place in TextEdit
tell application “TextEdit”
launch
activate
make new document with properties {text:theText}
end tell

####################################################################Place in Excel
set the clipboard to (theText)
tell application “Microsoft Excel”
launch
activate
make new workbook
activate workbook
select sheet 1 of active workbook
select range “A1” of active sheet
paste special on worksheet sheet 1
end tell