Synced diff-scripts with TortoiseSVN
[TortoiseGit.git] / contrib / diff-scripts / diff-doc.js
blob993970c90667626f6359cccf2eb92e2ad7cd735f
1 // extensions: doc;docx;docm\r
2 //\r
3 // TortoiseSVN Diff script for Word Doc files\r
4 //\r
5 // Copyright (C) 2004-2008, 2011, 2013 the TortoiseSVN team\r
6 // This file is distributed under the same license as TortoiseSVN\r
7 //\r
8 // Last commit by:\r
9 // $Author$\r
10 // $Date$\r
11 // $Rev$\r
12 //\r
13 // Authors:\r
14 // Stefan Kueng, 2011, 2013\r
15 // Jared Silva, 2008\r
16 // Davide Orlandi and Hans-Emil Skogh, 2005\r
17 //\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
26 // WdCompareTarget\r
27 //var wdCompareTargetSelected = 0;\r
28 //var wdCompareTargetCurrent = 1;\r
29 var wdCompareTargetNew = 2;\r
30 // WdViewType\r
31 var wdMasterView = 5;\r
32 var wdNormalView = 1;\r
33 var wdOutlineView = 2;\r
34 // WdSaveOptions\r
35 var wdDoNotSaveChanges = 0;\r
36 //var wdPromptToSaveChanges = -2;\r
37 //var wdSaveChanges = -1;\r
39 objArgs = WScript.Arguments;\r
40 num = objArgs.length;\r
41 if (num < 2)\r
42 {\r
43     WScript.Echo("Usage: [CScript | WScript] diff-doc.js base.doc new.doc");\r
44     WScript.Quit(1);\r
45 }\r
47 sBaseDoc = objArgs(0);\r
48 sNewDoc = objArgs(1);\r
50 objScript = new ActiveXObject("Scripting.FileSystemObject");\r
52 if (!objScript.FileExists(sBaseDoc))\r
53 {\r
54     WScript.Echo("File " + sBaseDoc + " does not exist.  Cannot compare the documents.");\r
55     WScript.Quit(1);\r
56 }\r
58 if (!objScript.FileExists(sNewDoc))\r
59 {\r
60     WScript.Echo("File " + sNewDoc + " does not exist.  Cannot compare the documents.");\r
61     WScript.Quit(1);\r
62 }\r
64 try\r
65 {\r
66     word = WScript.CreateObject("Word.Application");\r
68     if (parseInt(word.Version, 10) >= vOffice2013)\r
69     {\r
70         var f = objScript.GetFile(sBaseDoc);\r
71         if (f.attributes & 1)\r
72         {\r
73             f.attributes = f.attributes - 1;\r
74         }\r
75     }\r
76 }\r
77 catch (e)\r
78 {\r
79     // before giving up, try with OpenOffice\r
80     var OO;\r
81     try\r
82     {\r
83         OO = WScript.CreateObject("com.sun.star.ServiceManager");\r
84     }\r
85     catch (e)\r
86     {\r
87         WScript.Echo("You must have Microsoft Word or OpenOffice installed to perform this operation.");\r
88         WScript.Quit(1);\r
89     }\r
90     // yes, OO is installed - do the diff with that one instead\r
91     var objFile = objScript.GetFile(sNewDoc);\r
92     if ((objFile.Attributes & 1) === 1)\r
93     {\r
94         // reset the readonly attribute\r
95         objFile.Attributes = objFile.Attributes & (~1);\r
96     }\r
97     //Create the DesktopSet\r
98     var objDesktop = OO.createInstance("com.sun.star.frame.Desktop");\r
99     var objUriTranslator = OO.createInstance("com.sun.star.uri.ExternalUriReferenceTranslator");\r
100     //Adjust the paths for OO\r
101     sBaseDoc = sBaseDoc.replace(/\\/g, "/");\r
102     sBaseDoc = sBaseDoc.replace(/:/g, "|");\r
103     sBaseDoc = sBaseDoc.replace(/ /g, "%20");\r
104     sBaseDoc = sBaseDoc.replace(/#/g, "%23");\r
105     sBaseDoc = "file:///" + sBaseDoc;\r
106     sBaseDoc = objUriTranslator.translateToInternal(sBaseDoc);\r
107     sNewDoc = sNewDoc.replace(/\\/g, "/");\r
108     sNewDoc = sNewDoc.replace(/:/g, "|");\r
109     sNewDoc = sNewDoc.replace(/ /g, "%20");\r
110     sNewDoc = sNewDoc.replace(/#/g, "%23");\r
111     sNewDoc = "file:///" + sNewDoc;\r
112     sNewDoc = objUriTranslator.translateToInternal(sNewDoc);\r
114     //Open the %base document\r
115     var oPropertyValue = [];\r
116     oPropertyValue[0] = OO.Bridge_GetStruct("com.sun.star.beans.PropertyValue");\r
117     oPropertyValue[0].Name = "ShowTrackedChanges";\r
118     oPropertyValue[0].Value = true;\r
119     // objDocument is needed\r
120     var objDocument = objDesktop.loadComponentFromURL(sNewDoc,"_blank", 0, oPropertyValue);\r
122     //Set the frame\r
123     var Frame = objDesktop.getCurrentFrame();\r
125     var dispatcher = OO.CreateInstance("com.sun.star.frame.DispatchHelper");\r
127     //Execute the comparison\r
128     dispatcher.executeDispatch(Frame, ".uno:ShowTrackedChanges", "", 0, oPropertyValue);\r
129     oPropertyValue[0].Name = "URL";\r
130     oPropertyValue[0].Value = sBaseDoc;\r
131     dispatcher.executeDispatch(Frame, ".uno:CompareDocuments", "", 0, oPropertyValue);\r
132     WScript.Quit(0);\r
135 if (parseInt(word.Version, 10) >= vOffice2007)\r
137     sTempDoc = sNewDoc;\r
138     sNewDoc = sBaseDoc;\r
139     sBaseDoc = sTempDoc;\r
142 objScript = null;\r
143 word.visible = true;\r
145 // Open the new document\r
146 try\r
148     destination = word.Documents.Open(sNewDoc, true, (parseInt(word.Version, 10) < vOffice2013));\r
150 catch (e)\r
152     try\r
153     {\r
154         // open empty document to prevent bug where first Open() call fails\r
155         word.Documents.Add();\r
156         destination = word.Documents.Open(sNewDoc, true, (parseInt(word.Version, 10) < vOffice2013));\r
157     }\r
158     catch (e)\r
159     {\r
160         WScript.Echo("Error opening " + sNewDoc);\r
161         // Quit\r
162         WScript.Quit(1);\r
163     }\r
166 // If the Type property returns either wdOutlineView or wdMasterView and the Count property returns zero, the current document is an outline.\r
167 if (((destination.ActiveWindow.View.Type === wdOutlineView) || (destination.ActiveWindow.View.Type === wdMasterView)) && (destination.Subdocuments.Count === 0))\r
169     // Change the Type property of the current document to normal\r
170     destination.ActiveWindow.View.Type = wdNormalView;\r
173 // Compare to the base document\r
174 if (parseInt(word.Version, 10) <= vOffice2000)\r
176     // Compare for Office 2000 and earlier\r
177     try\r
178     {\r
179         destination.Compare(sBaseDoc);\r
180     }\r
181     catch (e)\r
182     {\r
183         WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);\r
184         // Quit\r
185         WScript.Quit(1);\r
186     }\r
188 else\r
190     // Compare for Office XP (2002) and later\r
191     try\r
192     {\r
193         destination.Compare(sBaseDoc, "Comparison", wdCompareTargetNew, true, true);\r
194     }\r
195     catch (e)\r
196     {\r
197         WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);\r
198         // Close the first document and quit\r
199         destination.Close(wdDoNotSaveChanges);\r
200         WScript.Quit(1);\r
201     }\r
204 // Show the comparison result\r
205 if (parseInt(word.Version, 10) < vOffice2007)\r
207     word.ActiveDocument.Windows(1).Visible = 1;\r
210 // Mark the comparison document as saved to prevent the annoying\r
211 // "Save as" dialog from appearing.\r
212 word.ActiveDocument.Saved = 1;\r
214 // Close the first document\r
215 if (parseInt(word.Version, 10) >= vOffice2002)\r
217     destination.Close(wdDoNotSaveChanges);\r