Could not save changes to script because stack oevrflowed

i have a script that does quite abit in memory that is, it scans a 3000+ file and cross references all against the contents of a folder.

But now it doesnt work and everytime that i try it i get the topic error message.
–ascii art


____________________________
|      |       Could not save changes to "script" 
| __|       because the scripts stack over flowed.
|
| -2706                     [ok button]
|____________________________
 this should be a message box.  Sorry for the ba drawing!!

Stack errors are usually due to errant recursive subroutines but can also be caused by memory bloat.

First check any recursive routines (ones that call themselves). Check, especially, for variable usage within them, and parameter passing from the parent to the child. If you’re passing a huge list into the recursive routine, that can cause problems - you’d be better off in that case using a global variable.

Also, are you using properties in your script?
If so, check them closely and see if you need them, or whether regular variables would work just as well.
Since properties are persistent, they retain their values between execution and can add to bloat.

Additionally, check your variable usage, especially scope - make sure you only define variables where you need them, and use subroutines to divide up your script into parts.

Also, clear large variable objects that you no longer need. e.g. if you have a large list that you no longer need, set it to an empty list to free up the memory it was using.

have been looking through my code following what you said i think that i have solved the problem. I no longer get the stack overflow, well dont seem to anyway.

But my code has a bug i cant find. Basically it should delete a file from the folder if it is in the txt document, i know its there as i made sure the filename was but it wont recognise the filename, and won’t delete the file from the folder.


set the_log_file to "Shared G4 HD:Users:karrenmay:Desktop:libraryLog.txt" as alias
set the_file to "Shared G4 HD:Users:karrenmay:Desktop:lbraryno.txt" as alias -- a text file with the file names each on a seperate line, one per line 
set all_folders to {"Shared G4 HD:Users:karrenmay:Desktop:web pics:"}
--------------------------------------------------------
-- This checks a folders files to see if they have been uploaded
-- If the file han't been then it will send the filename toa log file
-- Created by Andrew Davey with help from macscripter.net
--------------------------------------------------------

set list_from_file to read the_file using delimiter return

repeat with j from 1 to (count of all_folders)
	set the_folder to (item j of all_folders) as alias
	set list_from_folder to list folder the_folder without invisibles
	
	repeat with i from 1 to (count of list_from_folder)
		set the_item to (item i of list_from_folder) as string
		if list_from_file does not contain the_item then my add_item_to_file(the_log_file, the_item)
		if list_from_file contains the_item then my removeTheFile((the_folder as string) & the_item)
	end repeat
end repeat

on add_item_to_file(the_file, the_item)
	try
		open for access the_file with write permission
		write (the_item & return) to the_file starting at eof
		close access the_file
	on error
		try
			close access the_file
		end try
	end try
end add_item_to_file



on removeTheFile(the_item)
	try
		tell application "Finder" to delete (the_item as alias)
	on error
		display dialog "Could not delete " & the_item & ""
	end try
end removeTheFile

--reset the lists
set list_from_file to ""
set fileList to {}

Folder contents:
Floating.Market.7.jpg

txt file:
Floating.Market.7.jpg

Identical filename and it will not delete. The txt doc has the filename near the end, line number 4420 i think, rougthly.

But it just doesn’t recognise that it has been uploaded.

Your code works fine here so the problem must be in the file of filenames you’re cross-referencing.

What happens when you run the script? According to the code, if the removeTheFile handler is called, either the file gets removed or you get an error dialog. Do you see the dialog? If not, and the file isn’t removed then the removeTheFile handler isn’t getting called.

Does the ‘add_item_to_file’ code work? If that doesn’t work the problem is likely to be in the file of filenames.

Are you sure the text file is return-delimited and not line feed-based (as is the standard for Unix apps). Can you add a debug (display dialog) in the main loop to see what ‘the_item’ is? that will help determine if you’re comparing the right strings and help track down where the problem is.

Also, on a side note, you can almost double the speed of the script by changing these two lines:

      if list_from_file does not contain the_item then my add_item_to_file(the_log_file, the_item) 
      if list_from_file contains the_item then my removeTheFile((the_folder as string) & the_item) 

to:

      if list_from_file does not contain the_item then 
            my add_item_to_file(the_log_file, the_item) 
      else
            my removeTheFile((the_folder as string) & the_item) 
      end if

In your original script, you walk through the entire list twice, once to see if it does not contain the filename, and then again to see if it does. Simple logic says that if one is true the other can not be, so an else clause lets you scan the list once and take the appropriate actions.

This tip was already given to the poster but once again, he has cross-posted and not taken advice already given (see this post.)

Jon

sorry for the cross posting, i have solved the problem and it was working even from the word go. the problem i had was that the txt file didn’t always have the .jpg at the end.

So am now in the process of rewriting the txt doc with the extra .jpg.

Chgeers and sorry again

:oops: