saving environment for debugging purposes

After a bit of sweat and tears (see http://macscripter.net/viewtopic.php?pid=127236#p127236),

I found a way to write a save_environment() routine that will save to a file the content of some variables. And another one recover_environment() that will bring them back in another script

e.g

set variable1 to "My data to save"
set variable2 to "My other data to save"

save_environment({variable1, variable2)} -- saves the variables with name and content in a file on desktop

then

set {variable1, variable2} to recover_environment()

--is equivalent to :
set variable1 to "My data to save"
set variable2 to "My other data to save"

here it is:

(*
This script is a library defining two handlers for saving and recovering environments of variables

It saves to a file the variables you need, then, recover them in another script 
It is useful for debugging purposes when you want to split the execution of the program between the part where there is no bug, and where you can save the variables, and the one where there is a bug, that you want to work on. Especially if the first part takes a long time to compute.

You need to copy the routines inside the body of the program you want to debug
*)

(*
-- Saving is invoked as follows 
set variable1 to "Hello."
set variable2 to "How are you today?"
save_environment({"variable1", "variable2"}) -- any number of variables you want
*)

on save_environment(theList)
	set thefile to (path to desktop as text) & "MyEnvironment.file" -- this could also be set as a parameter
	try
		set thedata to open for access thefile with write permission
		set eof of thedata to 0
		set myVarList to {}
		repeat with myVarName in theList
			set end of myVarList to evaluate(myVarName)
		end repeat
		write (myVarList) to thedata starting at eof as list
		close access thedata
		return true
	on error e
		try
			close access thedata
		end try
		log e
		return false
	end try
end save_environment

to evaluate(theThing)
	run script "on run {x}
return x's " & theThing & "
end" with parameters {me}
end evaluate



--- Next is for recovering
(*
--Recovering is invoked by 
set {variable1, variable2} to recover_environment()
*)

on recover_environment()
	set thefile to (path to desktop as text) & "MyEnvironment.file" -- this could also be set as a parameter
	return (read file thefile as list)
end recover_environment

This seems like a real good solution as the other way to achieve the same would be to declare a script object which
you could save and load, but would lead to excessive editing.

It’s great utilities to have in the arsenal :confused:

McUsr

I wanted to promote an alternative way to load the above script as a library by invoking at the beginning of the program:


set MyLibrary to (load script file ((path to desktop as text) & "MyEnvironment.scpt"))
--assummed MyEnvironment.scpt has been saved on desktop

-- Saving is invoked by 
MyLibrary's save_environment({"variable1", "variable2"})

-- Recovering is invoked by 
set {variable1, variable2} to MyLibrary's recover_environment()

It’s more elegant because you don’t have to copy/paste the routines inside the program to debug, but there is still a bug.
The evaluate(theThing) function needs to be internalized inside the save_environment(theList) function, which is not so easy , so i let it to anyone who wants…

I think “my” evaluate( … ) " would do the trick.
I think I these routines can be really useful.

Mc Usr