1 // extensions: doc;docx;docm;dot;dotx;odt;ods
\r
3 // TortoiseSVN Diff script for Word Doc files
\r
5 // Copyright (C) 2004-2008, 2011, 2013, 2019 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 word.AutomationSecurity = 3; //msoAutomationSecurityForceDisable
\r
71 if (parseInt(word.Version, 10) >= vOffice2013)
\r
73 var f = objScript.GetFile(sBaseDoc);
\r
74 if (f.attributes & 1)
\r
76 f.attributes = f.attributes - 1;
\r
82 // before giving up, try with OpenOffice
\r
86 OO = WScript.CreateObject("com.sun.star.ServiceManager");
\r
90 WScript.Echo("You must have Microsoft Word or OpenOffice installed to perform this operation.");
\r
93 // yes, OO is installed - do the diff with that one instead
\r
94 var objFile = objScript.GetFile(sNewDoc);
\r
95 if ((objFile.Attributes & 1) === 1)
\r
97 // reset the readonly attribute
\r
98 objFile.Attributes = objFile.Attributes & (~1);
\r
100 //Create the DesktopSet
\r
101 var objDesktop = OO.createInstance("com.sun.star.frame.Desktop");
\r
102 var objUriTranslator = OO.createInstance("com.sun.star.uri.ExternalUriReferenceTranslator");
\r
103 //Adjust the paths for OO
\r
104 sBaseDoc = sBaseDoc.replace(/\\/g, "/");
\r
105 sBaseDoc = sBaseDoc.replace(/:/g, "|");
\r
106 sBaseDoc = sBaseDoc.replace(/ /g, "%20");
\r
107 sBaseDoc = sBaseDoc.replace(/#/g, "%23");
\r
108 sBaseDoc = "file:///" + sBaseDoc;
\r
109 sBaseDoc = objUriTranslator.translateToInternal(sBaseDoc);
\r
110 sNewDoc = sNewDoc.replace(/\\/g, "/");
\r
111 sNewDoc = sNewDoc.replace(/:/g, "|");
\r
112 sNewDoc = sNewDoc.replace(/ /g, "%20");
\r
113 sNewDoc = sNewDoc.replace(/#/g, "%23");
\r
114 sNewDoc = "file:///" + sNewDoc;
\r
115 sNewDoc = objUriTranslator.translateToInternal(sNewDoc);
\r
117 //Open the %base document
\r
118 var oPropertyValue = [];
\r
119 oPropertyValue[0] = OO.Bridge_GetStruct("com.sun.star.beans.PropertyValue");
\r
120 oPropertyValue[0].Name = "ShowTrackedChanges";
\r
121 oPropertyValue[0].Value = true;
\r
122 // objDocument is needed
\r
123 var objDocument = objDesktop.loadComponentFromURL(sNewDoc,"_blank", 0, oPropertyValue);
\r
126 var Frame = objDesktop.getCurrentFrame();
\r
128 var dispatcher = OO.CreateInstance("com.sun.star.frame.DispatchHelper");
\r
130 //Execute the comparison
\r
131 dispatcher.executeDispatch(Frame, ".uno:ShowTrackedChanges", "", 0, oPropertyValue);
\r
132 oPropertyValue[0].Name = "URL";
\r
133 oPropertyValue[0].Value = sBaseDoc;
\r
134 dispatcher.executeDispatch(Frame, ".uno:CompareDocuments", "", 0, oPropertyValue);
\r
138 if (parseInt(word.Version, 10) >= vOffice2007)
\r
140 sTempDoc = sNewDoc;
\r
141 sNewDoc = sBaseDoc;
\r
142 sBaseDoc = sTempDoc;
\r
146 word.visible = true;
\r
148 // Open the new document
\r
151 destination = word.Documents.Open(sNewDoc, true, (parseInt(word.Version, 10) < vOffice2013));
\r
157 // open empty document to prevent bug where first Open() call fails
\r
158 word.Documents.Add();
\r
159 destination = word.Documents.Open(sNewDoc, true, (parseInt(word.Version, 10) < vOffice2013));
\r
163 WScript.Echo("Error opening " + sNewDoc);
\r
169 // If the Type property returns either wdOutlineView or wdMasterView and the Count property returns zero, the current document is an outline.
\r
170 if ((destination.ActiveWindow.View.Type === wdOutlineView) || ((destination.ActiveWindow.View.Type === wdMasterView) || (destination.ActiveWindow.View.Type === wdReadingView)) && (destination.Subdocuments.Count === 0))
\r
172 // Change the Type property of the current document to normal
\r
173 destination.ActiveWindow.View.Type = wdNormalView;
\r
176 // Compare to the base document
\r
177 if (parseInt(word.Version, 10) <= vOffice2000)
\r
179 // Compare for Office 2000 and earlier
\r
182 destination.Compare(sBaseDoc);
\r
186 WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);
\r
193 // Compare for Office XP (2002) and later
\r
196 destination.Compare(sBaseDoc, "Comparison", wdCompareTargetNew, true, true);
\r
200 WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);
\r
201 // Close the first document and quit
\r
202 destination.Close(wdDoNotSaveChanges);
\r
207 // Show the comparison result
\r
208 if (parseInt(word.Version, 10) < vOffice2007)
\r
210 word.ActiveDocument.Windows(1).Visible = 1;
\r
213 // Mark the comparison document as saved to prevent the annoying
\r
214 // "Save as" dialog from appearing.
\r
215 word.ActiveDocument.Saved = 1;
\r
217 // Close the first document
\r
218 if (parseInt(word.Version, 10) >= vOffice2002)
\r
220 destination.Close(wdDoNotSaveChanges);
\r