exceeding character limit

Hi all.

Does anyone know of a way to get around the character limit in Appleccript. I am doing numerous find/replaces in a text file and I end up exceeding the character limit. I tried to break it down into multiple scripts and load and run them from a central script but then I get out of memory errors when the second script gets loaded.

I have it working with 4 separate scripts but I was hoping to simplify the process so only 1 script would need to be activated.

Any thoughts? (Besides getting the vendor to give me the output file in the corrct format to begin with).


Perhaps you can try (and adapt if needed) the following routine:

Make search/replace operations in data fork of a file (eg, plain text file)

s: search term
r: replace term
f: file path, alias, posix path (input file to make search/replacements in, only data fork)
f2: file path, alias, posix path (output file)

You can also adjust the property "chunk" in the "nesteed" script object (the number of bytes to read every time)
The more ocurrences you expect to search/replace, the shortest number in property "chunk" (eg, 100 Kb or 10 Kb).
If you are working in the classic environment, the limit is (aprox.): 25000 bytes for "chunk" and 4000 items for lists (eg, if you expect about 5000 search/replacements in 25000 bytes, it will crash).

fsearchReplace("vaca", "perro", alias "path:to:inputFile.txt", "path:to:outputFile.txt")

to fsearchReplace(s, r, f, f2)
	local s, r, f, f2, fRef, rwd, currentPosition, currentChunk, msg, n
	script nesteed
		property chunk : 500 * 1024 --> 500 Kb, fine for typical operations
		to searchReplace(s, r, t)
			set oldTids to AppleScript's text item delimiters
			set AppleScript's text item delimiters to s
			set t to t's text items
			set AppleScript's text item delimiters to r
			set t to t as text
			set AppleScript's text item delimiters to oldTids
		end searchReplace
	end script
	set f to f as Unicode text
	if f does not contain ":" then set f to POSIX file f as Unicode text
	set f to alias f --> make sure the file exists or throw error
	set f2 to f2 as Unicode text
	if f2 does not contain ":" then set f2 to POSIX file f2 as Unicode text
	--> open and empty output file
	set fRef to (open for access file f2 with write permission)
	set eof of fRef to 0
	try --> if anything is wrong, close output file
		set rwd to (count s) --> avoid missing parts
		set currentPosition to 1 --> position to start reading
			--> pick a chunk of text
				set currentChunk to (read f from currentPosition to (currentPosition + (nesteed's chunk)))
			on error
					set currentChunk to (read f from currentPosition to -1)
				on error --> no more text
					exit repeat
				end try
			end try
			write (nesteed's searchReplace(s, r, currentChunk)) to fRef starting at eof
			set currentPosition to currentPosition + (nesteed's chunk) - rwd
		end repeat
		close access fRef
	on error msg number n
			close access fRef
		end try
		error msg number n
	end try
end fsearchReplace