I have a script which works fine... except that it repeatedly crashes UltraEdit. I'm thinking memory allocation problems and have tried to code around the activedocument.selection problem, but may not have done this sufficiently well. Alternatively, there may be other known problems I'm tripping over. Or I may need to report a new error to IDM. All advice welcome!
UltraEdit Version 21.30.0.1024 and Windows 8.1 64-bit.
(N.B. code also includes latest versions of FIndSelectInnerOuter.js, FileNameFunctions.js and GetListOfFiles.js) but I have cut these out for brevity.)
UltraEdit Version 21.30.0.1024 and Windows 8.1 64-bit.
(N.B. code also includes latest versions of FIndSelectInnerOuter.js, FileNameFunctions.js and GetListOfFiles.js) but I have cut these out for brevity.)
Code: Select all
/* processtreefiles.js
This script processes the html output of two applications to produce a hybrid set of html.
It takes as input:
- two directories containing websites (gedmilldir and fhdir) each with 1000s or 10s of 1000s of files
and creates as output:
- a re-used directory containing the modified fh website (fhdir)
*/
// *** Parameters governing the script behaviours -- edit and select these as required -- these are NOT regular expressions ***
var gedmilldir = "C:\\Users\\Helen\\Genealogy\\Website FH content\\GEDmill\\"; //directory containing GEDmill files
var fhdir = "C:\\Users\\Helen\\Genealogy\\Website FH content\\FH\\"; //directory containing Family Historian files
var treefiletype = "png"; //should be gif or png -- whatever is being used for GEDmill tree diagrams
// ***String constants***
// *** Shouldn't need to edit these unless GEDmill or Family Historian change the content or format of their output or MZP changes its installation instructions***
// *** These are NOT regular expressions ***
var gedmillminitreestart = "<div id=\"minitree\"" ;
var gedmillminitreeend = "</div>";
var fhseealsostart = "<div class=\"FhSeeAlso\">"; // the See also div identifier in the raw html files from FH web production.
var fhseealsoend = "</div>"; // end of the div.
var fhseealsostart2 = "<div class=\"fhcontent fhpageInd\">"; //Alternative start for the insertion point for the minitree
var fhseealsoend2 = "<h1 class=\"FhHdg1\">"; //and end
var lyteboxheadcss = "lytebox.css";
var lyteboxheadjs= "lytebox.js";
var mzpheadcss = "MagicZoomPlus.css";
var mzpheadjs= "MagicZoomPlus.js";
var lyteboxclass = "rel=\"lytebox\"";
var mzpclass = "class = \"MagicZoomPlus\"";
var navregex = "<li><span><a href=\"index.html\">Home</a></span></li>";
// ** Following variable defines the error reporting behaviours in some functions **
var DebugMessage = 1; //Enable error reporting, and use message boxes. Set to 1 to use the output window, to 2 to use message boxes and to 0 or any other value to not report errors
// Define a set of functions -- general utilities first
function ReportError(ErrorString,ErrorSource) {
/* Determine the type of output for debug messages from the global
variable DebugMessage: 1 ... debug to output window, 2 ... debug
to message dialog, all others ... no debug messages. If the global
variable DebugMessage does not exist, don't show debug messages. */
var OutputType = (typeof(DebugMessage) == "number") ? DebugMessage : 0;
if (OutputType == 2) {
UltraEdit.messageBox(ErrorString,ErrorSource);
} else if (OutputType == 1) {
if (UltraEdit.outputWindow.visible === false) UltraEdit.outputWindow.showWindow(true);
UltraEdit.outputWindow.write(ErrorSource +": " +ErrorString);
}
}
function SetDefaultSearchHandling (){
UltraEdit.perlReOn(); //Use Perl Regular expressions
UltraEdit.activeDocument.findReplace.regExp = false; //Disable regular expressions in find and replace
UltraEdit.activeDocument.findReplace.matchCase = false; //case insensitive
UltraEdit.activeDocument.findReplace.replaceAll = false; //Replace only first instance of found strings in each file
UltraEdit.activeDocument.findReplace.searchDown=true;//Search down form current position in file
}
function GetSelectedTextViaClipboard () {//this is used to avoid the memory problems associated with document.selection
UltraEdit.selectClipboard(1);
UltraEdit.activeDocument.copy(); //Get the selection onto clipboard 1
var strtemp = UltraEdit.clipboardContent;
UltraEdit.clearClipboard;
return strtemp;
}
//Now functions specific to processing GEDmill- and FH-generated html
function FixGEDmillFileLinks () {
/*Fix the links originating from GEdmill files to point to their fh equivalents*/
UltraEdit.activeDocument.top();//since we don't know where the cursor is in the file concerned
SetDefaultSearchHandling();
UltraEdit.activeDocument.findReplace.regExp = true; //Enable regular expressions in find and replace
UltraEdit.activeDocument.findReplace.replaceAll = true; //Replace all instances of found strings in each file
UltraEdit.activeDocument.findReplace.replace("indiI(\\d+)\\.html","ind\\1\\.html");//Fix the links originating from GEdmill files to point to their fh equivalents
}
function ChangeLyteboxToMagicZoomPlus () {
/*Change all reference to Lytebox to use Magiczoomplus instead*/
SetDefaultSearchHandling();
UltraEdit.activeDocument.findReplace.replaceAll = true; //Replace all instances of found strings in each file
UltraEdit.activeDocument.top();//since we don't know where the cursor is in the file concerned
UltraEdit.activeDocument.findReplace.replace(lyteboxheadcss,mzpheadcss);
UltraEdit.activeDocument.top();//since we don't know where the cursor is in the file concerned
UltraEdit.activeDocument.findReplace.replace(lyteboxheadjs,mzpheadjs);
UltraEdit.activeDocument.top();//since we don't know where the cursor is in the file concerned
UltraEdit.activeDocument.findReplace.replace(lyteboxclass,mzpclass);
}
function RemoveNavigationString () {
SetDefaultSearchHandling();
UltraEdit.activeDocument.findReplace.regExp = true; //Enable regular expressions in find and replace
UltraEdit.activeDocument.top();
UltraEdit.activeDocument.findReplace.replace(navregex,"");
return true;
}
function ProcessNameIndexFile (InputDir) {
//Update the fh index of individuals to have the correct navigation string
var fhfilename = InputDir + "_nameindex.html"; //file name with path
UltraEdit.open(fhfilename); //Active document is FH index of individuals; position is top of file
RemoveNavigationString();
UltraEdit.closeFile(fhfilename,1);//close and save fh file
return true;
}
function ModifyActiveIndividualFile(fhstring,gedmillstring) {
UltraEdit.activeDocument.top();
UltraEdit.activeDocument.findReplace.replace(fhstring,gedmillstring );//Now replace the see also text in the FH file with the minitree from the gedmillfile
FixGEDmillFileLinks();
ChangeLyteboxToMagicZoomPlus();
RemoveNavigationString();
UltraEdit.closeFile(UltraEdit.activeDocument.path,1); //close and save active document
return true;
}
function ProcessIndividualFile (Individual) {
var gedmillfilename = gedmilldir + "indiI" + Individual +".html"; //construct the gedmill filename;
var fhfilename = fhdir + "ind" + Individual +".html"; //construct the fh filename;
UltraEdit.open(gedmillfilename); //Active document is gedmill
SetDefaultSearchHandling();
//Get the text wanted from the gedmill file
if (!FindSelectOuter (gedmillminitreestart,gedmillminitreeend,-1,false)) {//get minitree from gedmillfile that will be inserted into fh file
//String not found in gedmill file -- report error, close file and finish
ReportError("FH individual minitree not found: " + Individual,"ProcessIndividualFile");
UltraEdit.closeFile(UltraEdit.activeDocument.path,2); //close gedmill document without saving
return false;
}
else {
var gedmillstring = GetSelectedTextViaClipboard();
UltraEdit.closeFile(UltraEdit.activeDocument.path,2); //close gedmill document without saving
//Modify the fh file
UltraEdit.open(fhfilename); //Active document is fh
SetDefaultSearchHandling();
if (!FindSelectOuter (fhseealsostart,fhseealsoend,-1,false)) {//get individual text to be replaced in fh file
//Option 1 not found in fh file -- try option 2
if (!FindSelectInner(fhseealsostart2,fhseealsoend2,-1,false)) {
//Neither option found in fh file -- report error, close file and finish
ReportError("FH individual 'See also' text not found: " + Individual,"ProcessIndividualFile");
UltraEdit.closeFile(UltraEdit.activeDocument.path,2); //close fh file without saving
return false;
}
else {
ModifyActiveIndividualFile(GetSelectedTextViaClipboard(),gedmillstring);
}
}
else {
ModifyActiveIndividualFile(GetSelectedTextViaClipboard(),gedmillstring);
}
}
//Now copy the tree file
var gedmilltreename = gedmilldir + "treeI" + Individual +"." + treefiletype;
var fhtreename = fhdir + "treeI"+Individual + "." + treefiletype;
UltraEdit.open(gedmilltreename);//Active document
UltraEdit.saveAs(fhtreename);
UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
return true;
}
function ProcessFamilyTree () {
if (GetListOfFiles(0,gedmilldir,"indiI*.html",false)){
//Document [0] is now the list of GEDmill files.
//Process all the individuals' files
while (!UltraEdit.document[0].isEof()) { //Loop through the lines in this file until end of file is reached
SetDefaultSearchHandling();
UltraEdit.activeDocument.findReplace.regExp = true; //Enable regular expressions in find and replace
UltraEdit.document[0].selectLine();//read the filename from the next line
var ind=UltraEdit.document[0].selection.match(/\d+/);
ProcessIndividualFile(ind);
UltraEdit.document[0].key("RIGHT ARROW"); //move to the next line in the list of files
}
UltraEdit.closeFile(UltraEdit.document[0].path,2); //Close the file list without saving
}
else {
ReportError("NO GEDmill files. Terminating.","ProcessFamilyTree");
}
}
//Main script below here
UltraEdit.outputWindow.clear();//empty the output window
if (UltraEdit.document.length > 0) {
//First check that there are no open files to confuse the script, which relies on the order in which various files are opened.
ReportError("Please close open files before trying again","MainScript");
}
else {
//Process family tree contents
if (!ProcessNameIndexFile(fhdir)) {
//Error reporting was done in ProcessNameFileIndex
UltraEdit.closeFile(UltraEdit.document[0].path,2); //Close the file list without saving
}
else {
ProcessFamilyTree();
}
}