Splitting a bbedit file

Hi,

I have a bbedit 8 file I need to split and save into several smaller text files. I’ve been trying to get a test file to split using a search that puts CAT and DOG one after the other like this:
CAT

DOG
CAT

DOG

my script seems to getting hung up creating the unique filename. When I put in a static file name it works, but overwrites itself. Below is my script so far. Any help would be much appreciated.

Thanks,
Jon

tell application "BBEdit"
	
	activate
	
	try
		select insertion point before character 1 of text window 1
		set PathName to "Macintosh HD:"
		set filename to ""
		set filenumber to 1
		
		set stillfinding to true
		repeat while stillfinding
			set stillfinding to find "CAT" searching in text of text window 1 options {search mode:literal, starting at top:false, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false} with selecting match
			set stillfinding to the found of stillfinding
			if (stillfinding) then
				find "DOG" searching in text 1 of text window 1 options {search mode:literal, starting at top:false, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:true} with selecting match
				cut selection
				make new text window
				paste
				set filenumber to filename as text
				
				save text document 1 to file "Macintosh HD:" & filename & ".txt"
				close text document 1
				set filenumber to filenumber + 1
				
			end if
		end repeat
	end try
	
	
end tell

Model: G5
AppleScript: 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Moved to OS X from Mac OS (9)