// DocWordCount // // This is a script for Directory Opus. // See http://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information. // Called by Directory Opus to initialize the script function OnInit(initData) { initData.name = "Doc Word Count"; initData.desc = "Word count column for Microsoft Word documents. (Requires Microsoft Word.)"; initData.copyright = "(C) 2015 Leo Davidson"; initData.version = "1.0"; initData.default_enable = true; var col = initData.AddColumn(); col.name = "DocWordCount"; col.method = "OnDocWordCount"; col.label = "Word Count"; col.justify = "left"; col.autogroup = true; col.autorefresh = true; col.type = "number"; } var fsu = null; var wordApp = null; // Implement the DocWordCount column function OnDocWordCount(scriptColData) { var fileExt = scriptColData.item.ext; if (scriptColData.col != "DocWordCount" || (fileExt != ".doc" && fileExt != ".docx") || scriptColData.item.name[0] == "~") { return; } if (fsu == null) { fsu = DOpus.FSUtil; } if (wordApp == null) { try { wordApp = GetObject("", "Word.Application"); } catch(e) { wordApp = null; } if (wordApp == null) { wordApp = new ActiveXObject("Word.Application"); } if (wordApp == null) { DOpus.Output("Failed to open Word", true); return; } // wordApp.Visible = true; } var wordDocuments = wordApp.Documents; var filePath = scriptColData.item.realpath + ""; var wordFile = null; var closeFile = false; for(var docEnum = new Enumerator(wordDocuments); !docEnum.atEnd(); docEnum.moveNext()) { var fullPath = docEnum.item().Path + "\\" + docEnum.item().Name; if (fsu.ComparePath(fullPath, filePath)) { DOpus.Output("File already loaded: " + fullPath); wordFile = docEnum.item(); break; } } if (wordFile == null) { wordFile = wordDocuments.open(filePath, false, true, false); closeFile = true; DOpus.Output("Loaded: " + filePath); } if (wordFile != null) { scriptColData.value = wordFile.ReadabilityStatistics("words").Value; if (closeFile) { wordFile.Close(); } } }