1 // extensions: doc;docx;docm
\r
3 // TortoiseSVN Diff script for Word Doc files
\r
5 // Copyright (C) 2004-2008, 2011, 2013 the TortoiseSVN team
\r
6 // This file is distributed under the same license as TortoiseSVN
\r
14 // Stefan Kueng, 2011, 2013
\r
15 // Jared Silva, 2008
\r
16 // Davide Orlandi and Hans-Emil Skogh, 2005
\r
19 var objArgs, num, sBaseDoc, sNewDoc, sTempDoc, objScript, word, destination;
\r
20 // Microsoft Office versions for Microsoft Windows OS
\r
21 var vOffice2000 = 9;
\r
22 var vOffice2002 = 10;
\r
23 //var vOffice2003 = 11;
\r
24 var vOffice2007 = 12;
\r
25 var vOffice2013 = 15;
\r
27 //var wdCompareTargetSelected = 0;
\r
28 //var wdCompareTargetCurrent = 1;
\r
29 var wdCompareTargetNew = 2;
\r
31 var wdMasterView = 5;
\r
32 var wdNormalView = 1;
\r
33 var wdOutlineView = 2;
\r
35 var wdDoNotSaveChanges = 0;
\r
36 //var wdPromptToSaveChanges = -2;
\r
37 //var wdSaveChanges = -1;
\r
38 var wdReadingView = 7;
\r
40 objArgs = WScript.Arguments;
\r
41 num = objArgs.length;
\r
44 WScript.Echo("Usage: [CScript | WScript] diff-doc.js base.doc new.doc");
\r
48 sBaseDoc = objArgs(0);
\r
49 sNewDoc = objArgs(1);
\r
51 objScript = new ActiveXObject("Scripting.FileSystemObject");
\r
53 if (!objScript.FileExists(sBaseDoc))
\r
55 WScript.Echo("File " + sBaseDoc + " does not exist. Cannot compare the documents.");
\r
59 if (!objScript.FileExists(sNewDoc))
\r
61 WScript.Echo("File " + sNewDoc + " does not exist. Cannot compare the documents.");
\r
67 word = WScript.CreateObject("Word.Application");
\r
69 if (parseInt(word.Version, 10) >= vOffice2013)
\r
71 var f = objScript.GetFile(sBaseDoc);
\r
72 if (f.attributes & 1)
\r
74 f.attributes = f.attributes - 1;
\r
80 // before giving up, try with OpenOffice
\r
84 OO = WScript.CreateObject("com.sun.star.ServiceManager");
\r
88 WScript.Echo("You must have Microsoft Word or OpenOffice installed to perform this operation.");
\r
91 // yes, OO is installed - do the diff with that one instead
\r
92 var objFile = objScript.GetFile(sNewDoc);
\r
93 if ((objFile.Attributes & 1) === 1)
\r
95 // reset the readonly attribute
\r
96 objFile.Attributes = objFile.Attributes & (~1);
\r
98 //Create the DesktopSet
\r
99 var objDesktop = OO.createInstance("com.sun.star.frame.Desktop");
\r
100 var objUriTranslator = OO.createInstance("com.sun.star.uri.ExternalUriReferenceTranslator");
\r
101 //Adjust the paths for OO
\r
102 sBaseDoc = sBaseDoc.replace(/\\/g, "/");
\r
103 sBaseDoc = sBaseDoc.replace(/:/g, "|");
\r
104 sBaseDoc = sBaseDoc.replace(/ /g, "%20");
\r
105 sBaseDoc = sBaseDoc.replace(/#/g, "%23");
\r
106 sBaseDoc = "file:///" + sBaseDoc;
\r
107 sBaseDoc = objUriTranslator.translateToInternal(sBaseDoc);
\r
108 sNewDoc = sNewDoc.replace(/\\/g, "/");
\r
109 sNewDoc = sNewDoc.replace(/:/g, "|");
\r
110 sNewDoc = sNewDoc.replace(/ /g, "%20");
\r
111 sNewDoc = sNewDoc.replace(/#/g, "%23");
\r
112 sNewDoc = "file:///" + sNewDoc;
\r
113 sNewDoc = objUriTranslator.translateToInternal(sNewDoc);
\r
115 //Open the %base document
\r
116 var oPropertyValue = [];
\r
117 oPropertyValue[0] = OO.Bridge_GetStruct("com.sun.star.beans.PropertyValue");
\r
118 oPropertyValue[0].Name = "ShowTrackedChanges";
\r
119 oPropertyValue[0].Value = true;
\r
120 // objDocument is needed
\r
121 var objDocument = objDesktop.loadComponentFromURL(sNewDoc,"_blank", 0, oPropertyValue);
\r
124 var Frame = objDesktop.getCurrentFrame();
\r
126 var dispatcher = OO.CreateInstance("com.sun.star.frame.DispatchHelper");
\r
128 //Execute the comparison
\r
129 dispatcher.executeDispatch(Frame, ".uno:ShowTrackedChanges", "", 0, oPropertyValue);
\r
130 oPropertyValue[0].Name = "URL";
\r
131 oPropertyValue[0].Value = sBaseDoc;
\r
132 dispatcher.executeDispatch(Frame, ".uno:CompareDocuments", "", 0, oPropertyValue);
\r
136 if (parseInt(word.Version, 10) >= vOffice2007)
\r
138 sTempDoc = sNewDoc;
\r
139 sNewDoc = sBaseDoc;
\r
140 sBaseDoc = sTempDoc;
\r
144 word.visible = true;
\r
146 // Open the new document
\r
149 destination = word.Documents.Open(sNewDoc, true, (parseInt(word.Version, 10) < vOffice2013));
\r
155 // open empty document to prevent bug where first Open() call fails
\r
156 word.Documents.Add();
\r
157 destination = word.Documents.Open(sNewDoc, true, (parseInt(word.Version, 10) < vOffice2013));
\r
161 WScript.Echo("Error opening " + sNewDoc);
\r
167 // If the Type property returns either wdOutlineView or wdMasterView and the Count property returns zero, the current document is an outline.
\r
168 if ((destination.ActiveWindow.View.Type === wdOutlineView) || ((destination.ActiveWindow.View.Type === wdMasterView) || (destination.ActiveWindow.View.Type === wdReadingView)) && (destination.Subdocuments.Count === 0))
\r
170 // Change the Type property of the current document to normal
\r
171 destination.ActiveWindow.View.Type = wdNormalView;
\r
174 // Compare to the base document
\r
175 if (parseInt(word.Version, 10) <= vOffice2000)
\r
177 // Compare for Office 2000 and earlier
\r
180 destination.Compare(sBaseDoc);
\r
184 WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);
\r
191 // Compare for Office XP (2002) and later
\r
194 destination.Compare(sBaseDoc, "Comparison", wdCompareTargetNew, true, true);
\r
198 WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);
\r
199 // Close the first document and quit
\r
200 destination.Close(wdDoNotSaveChanges);
\r
205 // Show the comparison result
\r
206 if (parseInt(word.Version, 10) < vOffice2007)
\r
208 word.ActiveDocument.Windows(1).Visible = 1;
\r
211 // Mark the comparison document as saved to prevent the annoying
\r
212 // "Save as" dialog from appearing.
\r
213 word.ActiveDocument.Saved = 1;
\r
215 // Close the first document
\r
216 if (parseInt(word.Version, 10) >= vOffice2002)
\r
218 destination.Close(wdDoNotSaveChanges);
\r