bde4b451e763f0e0eba7765776c00b83841d6440
[TortoiseGit.git] / contrib / diff-scripts / diff-doc.js
blobbde4b451e763f0e0eba7765776c00b83841d6440
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;
120     //Set the frame
121     var Frame = objDesktop.getCurrentFrame();
123     var dispatcher = OO.CreateInstance("com.sun.star.frame.DispatchHelper");
125     //Execute the comparison
126     dispatcher.executeDispatch(Frame, ".uno:ShowTrackedChanges", "", 0, oPropertyValue);
127     oPropertyValue[0].Name = "URL";
128     oPropertyValue[0].Value = sBaseDoc;
129     dispatcher.executeDispatch(Frame, ".uno:CompareDocuments", "", 0, oPropertyValue);
130     WScript.Quit(0);
133 if (parseInt(word.Version) >= vOffice2007)
135     sTempDoc = sNewDoc;
136     sNewDoc = sBaseDoc;
137     sBaseDoc = sTempDoc;
140 objScript = null;
141 word.visible = true;
143 // Open the new document
146     destination = word.Documents.Open(sNewDoc, true, (parseInt(word.Version) < vOffice2013));
148 catch (e)
150     try
151     {
152         // open empty document to prevent bug where first Open() call fails
153         word.Documents.Add();
154         destination = word.Documents.Open(sNewDoc, true, (parseInt(word.Version) < vOffice2013));
155     }
156     catch (e)
157     {
158         WScript.Echo("Error opening " + sNewDoc);
159         // Quit
160         WScript.Quit(1);
161     }
164 // If the Type property returns either wdOutlineView or wdMasterView and the Count property returns zero, the current document is an outline.
165 if (((destination.ActiveWindow.View.Type === wdOutlineView) || (destination.ActiveWindow.View.Type === wdMasterView)) && (destination.Subdocuments.Count === 0))
167     // Change the Type property of the current document to normal
168     destination.ActiveWindow.View.Type = wdNormalView;
171 // Compare to the base document
172 if (parseInt(word.Version) <= vOffice2000)
174     // Compare for Office 2000 and earlier
175     try
176     {
177         destination.Compare(sBaseDoc);
178     }
179     catch (e)
180     {
181         WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);
182         // Quit
183         WScript.Quit(1);
184     }
186 else
188     // Compare for Office XP (2002) and later
189     try
190     {
191         destination.Compare(sBaseDoc, "Comparison", wdCompareTargetNew, true, true);
192     }
193     catch (e)
194     {
195         WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);
196         // Close the first document and quit
197         destination.Close(wdDoNotSaveChanges);
198         WScript.Quit(1);
199     }
202 // Show the comparison result
203 if (parseInt(word.Version) < vOffice2007)
205     word.ActiveDocument.Windows(1).Visible = 1;
208 // Mark the comparison document as saved to prevent the annoying
209 // "Save as" dialog from appearing.
210 word.ActiveDocument.Saved = 1;
212 // Close the first document
213 if (parseInt(word.Version) >= vOffice2002)
215     destination.Close(wdDoNotSaveChanges);