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