When I make changes to a handler in a script that I use as a script library, the next time I call that handler the changes are not always reflected. Quitting Script Debugger, restarting, doesn’t seem to help. I need to rename the handler, and that does the trick.
Does anyone know how to get around this problem?
Do the scripts which use the library actively load it at run time, or do they have it compiled into them as a property?
they are loaded at the beginning of the script thusly:
property SchrundLibrary : load script ("Macintosh HD:Users:Shared:automation:Script_Library:Schrund.scptd") as alias
property FormsLibrary : load script ("Macintosh HD:Users:Shared:automation:Script_Library:Forms.scptd") as alias
and then called:
set moduleOrder to SchrundLibrary's makeListFromCommaDelimit((SchrundLibrary's cellPull(2, 19, "S")))
That’s it then. The values of properties are set when a script’s compiled, so that’s when your libraries were loaded. When you subsequently edited the library scripts, it had no effect on the ‘script’ values stored in the properties until you recompiled the client script as well. One can surmise that changing the name of one of the library handlers forced you to change its name in the calling script too and it was recompiling after that which loaded the modified library.
You can avoid having to recompile your client scripts as well by having them load the libraries at run time, which will ensure they always use the latest version:
global SchrundLibrary, FormsLibrary
set SchrundLibrary to (load script ("Macintosh HD:Users:Shared:automation:Script_Library:Schrund.scptd") as alias)
set FormsLibrary to (load script ("Macintosh HD:Users:Shared:automation:Script_Library:Forms.scptd") as alias)
That did trick, Nigel. Thank you!
So if I understand it correctly, the only penalty to this approach is that it compiles at runtime versus using what is stored in memory?
If so, why doesn’t the cache get cleared upon restarting? Is there a way to force it to do so?
The operative word here is “loads”. In a property declaration, the ‘load’ command is only executed during the compilation of the script. With my code, it’s executed every time the script’s run.
I don’t know what you mean by “cache”. The closing values of a running script’s properties are saved back to the script file at the end of each run, so your client script file contains both sets of library code as well as its own. The same is actually true with globals and run-handler variables, so my code hasn’t stopped the bloat. It just reloads the libraries from the original files each time. (Obviously if you’re running the script in an editor, property and global values are only saved to the file if you save the script yourself after running it.)
You can prevent the library script objects from being stored in the client script file either by making the SchrundLibrary and FormsLibrary variables local instead of global, or by having the script set their values to something else ” say an empty string (“”) ” just before it exits.