I was trying to split a script into modules for the first time, but I can't get it to work.
For reference, here’s a simple example:
// Called by Directory Opus to initialize the script
function OnInit(initData) {
initData.name = 'dlgtest';
initData.version = '1.0';
initData.desc = '';
initData.default_enable = true;
initData.min_version = '13.16';
}
// Called to add commands to Opus
function OnAddCommands(addCmdData) {
var cmd = addCmdData.AddCommand();
cmd.name = 'dlgtest';
cmd.method = 'Ondlgtest';
cmd.desc = '';
cmd.label = 'dlgtest';
cmd.template = '';
cmd.hide = false;
cmd.icon = 'script';
}
// Implement the dlgtest command
function Ondlgtest(scriptCmdData) {
var dlg = scriptCmdData.func.Dlg();
dlg.template = 'dialog1';
dlg.title = 'module test';
dlg.Create();
var abc = GetVal();
dlg.Control("static1").label = abc.value;
dlg.RunDlg();
}
And the module:
function GetVal() {
return {
'value': 'This comes from a module!'
};
}
At first, when I ran the command, it threw an error saying the GetVal() function (which is in the module) is not defined. If I put the same function in the main script, it works fine.
Now, after closing and reopening the editor to try editing the script, the module no longer appears listed, and if I try to modify the script in any way, DOpus closes silently (the process just ends). No error log is created.
I haven't been able to reproduce the crash so far. (If you sent in crash dumps, it's possible they've already been looked at and fixed separately. If not, please send them in and let us know the IDs here.)
Making the module itself work can be done by doing inside the script's .osp file and either:
Rename dlgtest.js.txt to dlgtest.js (which has no downside; no reason to use the .txt suffix inside a .osp or .opusscriptinstall package).
Or rename dlgtest.js.test.osm to dlgtest.js.txt.test.osm. (Downside is the script editor UI will include the .txt the start of the module's name, although we can probably fix that. Will look at that in a moment. Edit: Will be in the next beta.)
Basically, the whole base script filename (including any extensions) has to be included at the start of all module filenames.
First thanks for taking the time to look into this. I really appreciate it.
There's no crash dumps/logs generated. Opus just silently closes.
Yep, that did the trick. Also fixes the script.
FWIW, I can reproduce the crash at will, by simply renaming back to dlgtest.js.txt. Then, when I open the Script Editor and try to edit anything (e.g. the main script), Opus crashes as soon as I save.
I did not create the package manually. I first created the script, selecting "use .txt extension". Then converted it to a package and added the module, all via the Script Editor.
Since there's no indication that modules won't work with that extra extension, maybe Opus could automatically strip ".txt" from the name when converting it to a package? For future use proof with other users. It seems that the ".txt" part is causing both the crash and the module issues.
Good point! I've fixed that for the next beta as well.
May have also found & fixed what was causing the crash, although it's just in theory as I haven't had it happen to me yet. I don't think it will be possible with the other changes anyway, but have fixed it anyway.