Install this script add-in to gain a Word Count column which will display the number of words in .doc and .docx files.
To Install:
- Download: DocWordCount.js.txt (2.16 KB)
- Drag the .js.txt file to Preferences / Toolbars / Scripts.
- Add the Scripts > Word Count column to your file display.
Requirements:
- You need to have Microsoft Word installed.
- So far only tested with Word 2010.
Notes / Warnings:
- The script talks to Microsoft Word to get statistics about the document. To do this, it has to ask Word to open the file, and then tells Word to close it when done.
- As a result, if you have Word open, you may see its windows flicker as files are loaded and unloaded.
- The script tries to detect if a document is already loaded into Word, and uses the existing open document if so, without closing it when done. There is a chance that detection won't always work, which might result in you being prompted to close the file you're editing (or having it close itself if there are no unsaved changes). If you notice anything strange happening within Word, close the Opus windows that have the column in them, and please report the problem here so the script might be improved.
Script code:
You do not need the code below -- a copy of it is in the download, above -- but it is here for people browsing the forum looking for scripting techniques:
// 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();
}
}
}