Prepared release and bumped version numbers to 1.8.8.0
[TortoiseGit.git] / contrib / diff-scripts / diff-doc.js
blob90c5b240d1986f168874bb2b8bf376faa1fd79cc
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
38 var wdReadingView = 7;\r
40 objArgs = WScript.Arguments;\r
41 num = objArgs.length;\r
42 if (num < 2)\r
43 {\r
44     WScript.Echo("Usage: [CScript | WScript] diff-doc.js base.doc new.doc");\r
45     WScript.Quit(1);\r
46 }\r
48 sBaseDoc = objArgs(0);\r
49 sNewDoc = objArgs(1);\r
51 objScript = new ActiveXObject("Scripting.FileSystemObject");\r
53 if (!objScript.FileExists(sBaseDoc))\r
54 {\r
55     WScript.Echo("File " + sBaseDoc + " does not exist.  Cannot compare the documents.");\r
56     WScript.Quit(1);\r
57 }\r
59 if (!objScript.FileExists(sNewDoc))\r
60 {\r
61     WScript.Echo("File " + sNewDoc + " does not exist.  Cannot compare the documents.");\r
62     WScript.Quit(1);\r
63 }\r
65 try\r
66 {\r
67     word = WScript.CreateObject("Word.Application");\r
69     if (parseInt(word.Version, 10) >= vOffice2013)\r
70     {\r
71         var f = objScript.GetFile(sBaseDoc);\r
72         if (f.attributes & 1)\r
73         {\r
74             f.attributes = f.attributes - 1;\r
75         }\r
76     }\r
77 }\r
78 catch (e)\r
79 {\r
80     // before giving up, try with OpenOffice\r
81     var OO;\r
82     try\r
83     {\r
84         OO = WScript.CreateObject("com.sun.star.ServiceManager");\r
85     }\r
86     catch (e)\r
87     {\r
88         WScript.Echo("You must have Microsoft Word or OpenOffice installed to perform this operation.");\r
89         WScript.Quit(1);\r
90     }\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
94     {\r
95         // reset the readonly attribute\r
96         objFile.Attributes = objFile.Attributes & (~1);\r
97     }\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
123     //Set the frame\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
133     WScript.Quit(0);\r
136 if (parseInt(word.Version, 10) >= vOffice2007)\r
138     sTempDoc = sNewDoc;\r
139     sNewDoc = sBaseDoc;\r
140     sBaseDoc = sTempDoc;\r
143 objScript = null;\r
144 word.visible = true;\r
146 // Open the new document\r
147 try\r
149     destination = word.Documents.Open(sNewDoc, true, (parseInt(word.Version, 10) < vOffice2013));\r
151 catch (e)\r
153     try\r
154     {\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
158     }\r
159     catch (e)\r
160     {\r
161         WScript.Echo("Error opening " + sNewDoc);\r
162         // Quit\r
163         WScript.Quit(1);\r
164     }\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
178     try\r
179     {\r
180         destination.Compare(sBaseDoc);\r
181     }\r
182     catch (e)\r
183     {\r
184         WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);\r
185         // Quit\r
186         WScript.Quit(1);\r
187     }\r
189 else\r
191     // Compare for Office XP (2002) and later\r
192     try\r
193     {\r
194         destination.Compare(sBaseDoc, "Comparison", wdCompareTargetNew, true, true);\r
195     }\r
196     catch (e)\r
197     {\r
198         WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);\r
199         // Close the first document and quit\r
200         destination.Close(wdDoNotSaveChanges);\r
201         WScript.Quit(1);\r
202     }\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