Assigning a new default TOC and then deleting the first TOC

In TCS2, this code that sets the second TOC as the default and then deletes the first TOC worked consistently.

if (RoboHelp.project.TOCManager.item(2).setAsDefault()) {

RoboHelp.project.TOCManager.item(1).delete();

}

In TCS4, the same code sometimes crashed the script, so I had to add this code close and then reopen the project, and then check whether the TOC that I’m trying to delete is the default, before deleting the old TOC. I suspect inserting a delay would achieve the same thing – if you try it and it works, let me know.

if (RoboHelp.project.TOCManager.item(2).setAsDefault()) {

var projectName = fsName(RoboHelp.project.path) + “\\” + RoboHelp.project.name + “.xpj”;
RoboHelp.closeProject();
RoboHelp.openProject(projectName);

if ( RoboHelp.project.TOCManager.item(1).name.indexOf(“Default”) == -1) {
RoboHelp.project.TOCManager.item(1).delete();
}
}