I have a series of script libraries (a string class, a file class, a date class, an OmniOutliner class, etc.) that call each other extensively. Originally, I would just have each class call whatever classes it needed when it was loaded, but this caused Stack Overflow (i.e. an infinite loop). So I tried creating a master classes class that is called whenever I need to load a class.
So in the top level document, I have:
set ClassCScpt to load script alias (((path to library folder) as text) & "Application Support:Applescript Classes:Classes Class.scpt")
ClassCScpt's new_Classes_class({"Application", "Project Time"}, {})
property classDebug : {}
property debugMode : true
The empty list is loadedCs, a variable to keep track of what classes had already been loaded, and not reload them.
Classes Class.scpt looks like this:
on new_Classes_class(classNameList, loadedCs)
if classNameList contains "Application" then
if loadedCs does not contain "AppC" then
global AppC
set filePath to ((path to library folder) as text) & "Application Support:Applescript Classes:Application Class.scpt"
set AppCScpt to load script file filePath
set loadedCs to loadedCs & "AppC"
set AppC to AppCScpt's new_Application_class(loadedCs)
end if
end if
-- repeat for each additional class
end new_Classes_class
The child classes look like this:
on new_Application_class(loadedCs)
set filePath to ((path to library folder) as text) & "Application Support:Applescript Classes:Classes Class.scpt"
set ClassCScpt to load script alias (filePath)
global AppC, LogC, logIt, FileC, StrC
if debugMode then set classDebug to classDebug & {{return & "loaded Application Class", loadedCs}}
ClassCScpt's new_Classes_class({"Application", "Logging", "File", "String"}, loadedCs)
script Application_class
--properties and subroutines here
end script
end new_Application_class
I can’t find a way to make loadedCs truly global, which is why it is currently a local variable that is passed into the function call. But will all of the nested calls, I can’t tell if loadedCs is keeping track of everything that’s been loaded. It worked for a while, but now I’m getting a mysterious “StrC (the string class variable) is not defined” error when I attempt to load any of my classes, and I don’t even know where to start in figuring out why that’s happening. I tried setting up a global variable in the script that calls that classes and using that to track which classes are loading and in what order, but I just get a “debugMode is not defined” error.
I can’t help thinking that I must be going about this the wrong way. Can anyone suggest a better way, or a way to debug my tangled web of scripts (I actually have a debugging class, which works pretty well when I can load my classes.) Help.
Thank you,
Rebecca