diff-doc.js: put back accidentally removed variable removed in TortoiseSVN revision...
[TortoiseGit.git] / contrib / diff-scripts / diff-doc.js
blob48443d0a0558d8919c89521fc35b44cbcaa769e5
1 // extensions: doc;docx;docm
2 //
3 // TortoiseSVN Diff script for Word Doc files
4 //
5 // Copyright (C) 2004-2008, 2011, 2013 the TortoiseSVN team
6 // This file is distributed under the same license as TortoiseSVN
7 //
8 // Last commit by:
9 // $Author$
10 // $Date$
11 // $Rev$
13 // Authors:
14 // Stefan Kueng, 2011, 2013
15 // Jared Silva, 2008
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
21 var vOffice2000 = 9;
22 var vOffice2002 = 10;
23 //var vOffice2003 = 11;
24 var vOffice2007 = 12;
25 var vOffice2013 = 15;
26 // WdCompareTarget
27 //var wdCompareTargetSelected = 0;
28 //var wdCompareTargetCurrent = 1;
29 var wdCompareTargetNew = 2;
30 // WdViewType
31 var wdMasterView = 5;
32 var wdNormalView = 1;
33 var wdOutlineView = 2;
34 // WdSaveOptions
35 var wdDoNotSaveChanges = 0;
36 //var wdPromptToSaveChanges = -2;
37 //var wdSaveChanges = -1;
39 objArgs = WScript.Arguments;
40 num = objArgs.length;
41 if (num < 2)
43     WScript.Echo("Usage: [CScript | WScript] diff-doc.js base.doc new.doc");
44     WScript.Quit(1);
47 sBaseDoc = objArgs(0);
48 sNewDoc = objArgs(1);
50 objScript = new ActiveXObject("Scripting.FileSystemObject");
52 if (!objScript.FileExists(sBaseDoc))
54     WScript.Echo("File " + sBaseDoc + " does not exist.  Cannot compare the documents.");
55     WScript.Quit(1);
58 if (!objScript.FileExists(sNewDoc))
60     WScript.Echo("File " + sNewDoc + " does not exist.  Cannot compare the documents.");
61     WScript.Quit(1);
64 try
66     word = WScript.CreateObject("Word.Application");
68     if (parseInt(word.Version) >= vOffice2013)
69     {
70         var f = objScript.GetFile(sBaseDoc);
71         if (f.attributes & 1)
72         {
73             f.attributes = f.attributes - 1;
74         }
75     }
77 catch (e)
79     // before giving up, try with OpenOffice
80     var OO;
81     try
82     {
83         OO = WScript.CreateObject("com.sun.star.ServiceManager");
84     }
85     catch (e)
86     {
87         WScript.Echo("You must have Microsoft Word or OpenOffice installed to perform this operation.");
88         WScript.Quit(1);
89     }
90     // yes, OO is installed - do the diff with that one instead
91     var objFile = objScript.GetFile(sNewDoc);
92     if ((objFile.Attributes & 1) === 1)
93     {
94         // reset the readonly attribute
95         objFile.Attributes = objFile.Attributes & (~1);
96     }
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);
122     //Set the frame
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);
132     WScript.Quit(0);
135 if (parseInt(word.Version) >= vOffice2007)
137     sTempDoc = sNewDoc;
138     sNewDoc = sBaseDoc;
139     sBaseDoc = sTempDoc;
142 objScript = null;
143 word.visible = true;
145 // Open the new document
148     destination = word.Documents.Open(sNewDoc, true, (parseInt(word.Version) < vOffice2013));
150 catch (e)
152     try
153     {
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));
157     }
158     catch (e)
159     {
160         WScript.Echo("Error opening " + sNewDoc);
161         // Quit
162         WScript.Quit(1);
163     }
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
177     try
178     {
179         destination.Compare(sBaseDoc);
180     }
181     catch (e)
182     {
183         WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);
184         // Quit
185         WScript.Quit(1);
186     }
188 else
190     // Compare for Office XP (2002) and later
191     try
192     {
193         destination.Compare(sBaseDoc, "Comparison", wdCompareTargetNew, true, true);
194     }
195     catch (e)
196     {
197         WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);
198         // Close the first document and quit
199         destination.Close(wdDoNotSaveChanges);
200         WScript.Quit(1);
201     }
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);