1 // extensions: doc;docx;docm
3 // TortoiseSVN Diff script for Word Doc files
5 // Copyright (C) 2004-2008, 2011, 2013 the TortoiseSVN team
6 // This file is distributed under the same license as TortoiseSVN
14 // Stefan Kueng, 2011, 2013
16 // Davide Orlandi and Hans-Emil Skogh, 2005
19 var objArgs, num, sBaseDoc, sNewDoc, sTempDoc, objScript, word, destination;
20 // Microsoft Office versions for Microsoft Windows OS
23 //var vOffice2003 = 11;
27 //var wdCompareTargetSelected = 0;
28 //var wdCompareTargetCurrent = 1;
29 var wdCompareTargetNew = 2;
33 var wdOutlineView = 2;
35 var wdDoNotSaveChanges = 0;
36 //var wdPromptToSaveChanges = -2;
37 //var wdSaveChanges = -1;
39 objArgs = WScript.Arguments;
43 WScript.Echo("Usage: [CScript | WScript] diff-doc.js base.doc new.doc");
47 sBaseDoc = objArgs(0);
50 objScript = new ActiveXObject("Scripting.FileSystemObject");
52 if (!objScript.FileExists(sBaseDoc))
54 WScript.Echo("File " + sBaseDoc + " does not exist. Cannot compare the documents.");
58 if (!objScript.FileExists(sNewDoc))
60 WScript.Echo("File " + sNewDoc + " does not exist. Cannot compare the documents.");
66 word = WScript.CreateObject("Word.Application");
68 if (parseInt(word.Version) >= vOffice2013)
70 var f = objScript.GetFile(sBaseDoc);
73 f.attributes = f.attributes - 1;
79 // before giving up, try with OpenOffice
83 OO = WScript.CreateObject("com.sun.star.ServiceManager");
87 WScript.Echo("You must have Microsoft Word or OpenOffice installed to perform this operation.");
90 // yes, OO is installed - do the diff with that one instead
91 var objFile = objScript.GetFile(sNewDoc);
92 if ((objFile.Attributes & 1) === 1)
94 // reset the readonly attribute
95 objFile.Attributes = objFile.Attributes & (~1);
97 //Create the DesktopSet
98 var objDesktop = OO.createInstance("com.sun.star.frame.Desktop");
99 var objUriTranslator = OO.createInstance("com.sun.star.uri.ExternalUriReferenceTranslator");
100 //Adjust the paths for OO
101 sBaseDoc = sBaseDoc.replace(/\\/g, "/");
102 sBaseDoc = sBaseDoc.replace(/:/g, "|");
103 sBaseDoc = sBaseDoc.replace(/ /g, "%20");
104 sBaseDoc = sBaseDoc.replace(/#/g, "%23");
105 sBaseDoc = "file:///" + sBaseDoc;
106 sBaseDoc = objUriTranslator.translateToInternal(sBaseDoc);
107 sNewDoc = sNewDoc.replace(/\\/g, "/");
108 sNewDoc = sNewDoc.replace(/:/g, "|");
109 sNewDoc = sNewDoc.replace(/ /g, "%20");
110 sNewDoc = sNewDoc.replace(/#/g, "%23");
111 sNewDoc = "file:///" + sNewDoc;
112 sNewDoc = objUriTranslator.translateToInternal(sNewDoc);
114 //Open the %base document
115 var oPropertyValue = new Array();
116 oPropertyValue[0] = OO.Bridge_GetStruct("com.sun.star.beans.PropertyValue");
117 oPropertyValue[0].Name = "ShowTrackedChanges";
118 oPropertyValue[0].Value = true;
119 // objDocument is needed
120 var objDocument = objDesktop.loadComponentFromURL(sNewDoc,"_blank", 0, oPropertyValue);
123 var Frame = objDesktop.getCurrentFrame();
125 var dispatcher = OO.CreateInstance("com.sun.star.frame.DispatchHelper");
127 //Execute the comparison
128 dispatcher.executeDispatch(Frame, ".uno:ShowTrackedChanges", "", 0, oPropertyValue);
129 oPropertyValue[0].Name = "URL";
130 oPropertyValue[0].Value = sBaseDoc;
131 dispatcher.executeDispatch(Frame, ".uno:CompareDocuments", "", 0, oPropertyValue);
135 if (parseInt(word.Version) >= vOffice2007)
145 // Open the new document
148 destination = word.Documents.Open(sNewDoc, true, (parseInt(word.Version) < vOffice2013));
154 // open empty document to prevent bug where first Open() call fails
155 word.Documents.Add();
156 destination = word.Documents.Open(sNewDoc, true, (parseInt(word.Version) < vOffice2013));
160 WScript.Echo("Error opening " + sNewDoc);
166 // If the Type property returns either wdOutlineView or wdMasterView and the Count property returns zero, the current document is an outline.
167 if (((destination.ActiveWindow.View.Type === wdOutlineView) || (destination.ActiveWindow.View.Type === wdMasterView)) && (destination.Subdocuments.Count === 0))
169 // Change the Type property of the current document to normal
170 destination.ActiveWindow.View.Type = wdNormalView;
173 // Compare to the base document
174 if (parseInt(word.Version) <= vOffice2000)
176 // Compare for Office 2000 and earlier
179 destination.Compare(sBaseDoc);
183 WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);
190 // Compare for Office XP (2002) and later
193 destination.Compare(sBaseDoc, "Comparison", wdCompareTargetNew, true, true);
197 WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);
198 // Close the first document and quit
199 destination.Close(wdDoNotSaveChanges);
204 // Show the comparison result
205 if (parseInt(word.Version) < vOffice2007)
207 word.ActiveDocument.Windows(1).Visible = 1;
210 // Mark the comparison document as saved to prevent the annoying
211 // "Save as" dialog from appearing.
212 word.ActiveDocument.Saved = 1;
214 // Close the first document
215 if (parseInt(word.Version) >= vOffice2002)
217 destination.Close(wdDoNotSaveChanges);