Trying to make a script to automatically delete lines from a text file

I use the junkmail program JunkMatcher with Mail in OS X, and it has an issue with it’s WhiteList and Python. So when it gets over 100 entries or 200 lines, it crashes Python. So I want to write a script that when I run it, it checks the text file to see if it has over say 150 or 160 lines and if it does it deletes ever line over 50 or so. I am not sure if this is more efficient to do in Applescript or if I should do this in TextEdit by counting paragraphs, but am having trouble.

Can anyone stear me in the right direction, as my attempts so far have been a total failure.

The Whitelist is a standard text file that uses returns, and each entry is 2 lines.

Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Shooting from the hip here(untested) so please make backups before using.

set _file to "path to file"
set _data to paragraphs of (read file _file)
if (count _data) > 150 then
	try
		close access _file
	end try
	set text item delimiters to return
	set _newData to items 1 through 50 of _data as string
	set _fileRef to (open for access file _file with write permission)
	set eof _fileRef to 0
	write _newData to _fileRef
	close access _fileRef
	set text item delimiters to ""
end if

Awesome, just tested that and it works perfectly. Thank you. Am going to disect it to understand as well. I really appreciate the help.

Glad it worked :smiley: I did make one change though to the above code. I had forgotten to set the text item delimiters back to the default which is always good practice.