Office2013 requires the diffed files to be opened writable
[TortoiseGit.git] / contrib / other / diff-scripts / diff-doc.js
blob4268b2d0446a865176734599127270556085b34a
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: tortoisesvn $\r
10 // $Date: 2012-10-11 20:47:11 +0200 (Do, 11. Okt 2012) $\r
11 // $Rev: 23366 $\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
51 if ( ! objScript.FileExists(sBaseDoc))\r
52 {\r
53     WScript.Echo("File " + sBaseDoc + " does not exist.  Cannot compare the documents.");\r
54     WScript.Quit(1);\r
55 }\r
56 if ( ! objScript.FileExists(sNewDoc))\r
57 {\r
58     WScript.Echo("File " + sNewDoc + " does not exist.  Cannot compare the documents.");\r
59     WScript.Quit(1);\r
60 }\r
62 try\r
63 {\r
64    word = WScript.CreateObject("Word.Application");\r
65 }\r
66 catch(e)\r
67 {\r
68         // before giving up, try with OpenOffice\r
69         try\r
70         {\r
71                 var OO;\r
72                 OO = WScript.CreateObject("com.sun.star.ServiceManager");\r
73         }\r
74         catch(e)\r
75         {\r
76                 WScript.Echo("You must have Microsoft Word or OpenOffice installed to perform this operation.");\r
77                 WScript.Quit(1);\r
78         }\r
79         // yes, OO is installed - do the diff with that one instead\r
80         var objFile = objScript.GetFile(sNewDoc);\r
81         if ((objFile.Attributes & 1)==1)\r
82         {\r
83                 // reset the readonly attribute\r
84                 objFile.Attributes = objFile.Attributes & (~1);\r
85         }\r
86         //Create the DesktopSet \r
87         var objDesktop = OO.createInstance("com.sun.star.frame.Desktop");\r
88         var objUriTranslator = OO.createInstance("com.sun.star.uri.ExternalUriReferenceTranslator");\r
89         //Adjust the paths for OO\r
90         sBaseDoc = sBaseDoc.replace(/\\/g, "/");\r
91         sBaseDoc = sBaseDoc.replace(/:/g, "|");\r
92         sBaseDoc = sBaseDoc.replace(/ /g, "%20");\r
93         sBaseDoc = sBaseDoc.replace(/#/g, "%23");\r
94         sBaseDoc="file:///" + sBaseDoc;\r
95         sBaseDoc=objUriTranslator.translateToInternal(sBaseDoc);\r
96         sNewDoc = sNewDoc.replace(/\\/g, "/");\r
97         sNewDoc = sNewDoc.replace(/:/g, "|");\r
98         sNewDoc = sNewDoc.replace(/ /g, "%20");\r
99         sNewDoc = sNewDoc.replace(/#/g, "%23");\r
100         sNewDoc="file:///" + sNewDoc;\r
101         sNewDoc=objUriTranslator.translateToInternal(sNewDoc);\r
103         //Open the %base document\r
104         var oPropertyValue = new Array();\r
105         oPropertyValue[0] = OO.Bridge_GetStruct("com.sun.star.beans.PropertyValue");\r
106         oPropertyValue[0].Name = "ShowTrackedChanges";\r
107         oPropertyValue[0].Value = true;\r
108         var objDocument=objDesktop.loadComponentFromURL(sNewDoc,"_blank", 0, oPropertyValue);\r
109         \r
110         //Set the frame\r
111         var Frame = objDesktop.getCurrentFrame();\r
112         \r
113         var dispatcher=OO.CreateInstance("com.sun.star.frame.DispatchHelper");\r
114         \r
115         //Execute the comparison\r
116         dispatcher.executeDispatch(Frame, ".uno:ShowTrackedChanges", "", 0, oPropertyValue);\r
117         oPropertyValue[0].Name = "URL";\r
118         oPropertyValue[0].Value = sBaseDoc;\r
119         dispatcher.executeDispatch(Frame, ".uno:CompareDocuments", "", 0, oPropertyValue);\r
120         WScript.Quit(0);\r
123 if (parseInt(word.Version) >= vOffice2007)\r
125         sTempDoc = sNewDoc;\r
126         sNewDoc = sBaseDoc;\r
127         sBaseDoc = sTempDoc;\r
130 objScript = null;\r
132 word.visible = true;\r
134 // Open the new document\r
135 try\r
137     destination = word.Documents.Open(sNewDoc, true, (parseInt(word.Version) < vOffice2013));\r
139 catch(e)\r
141     try\r
142     {\r
143         // open empty document to prevent bug where first Open() call fails\r
144         word.Documents.Add();\r
145         destination = word.Documents.Open(sNewDoc, true, (parseInt(word.Version) < vOffice2013));\r
146     }\r
147     catch(e)\r
148     {\r
149         WScript.Echo("Error opening " + sNewDoc);\r
150         // Quit\r
151         WScript.Quit(1);\r
152     }\r
155 // If the Type property returns either wdOutlineView or wdMasterView and the Count property returns zero, the current document is an outline.\r
156 if (((destination.ActiveWindow.View.Type == wdOutlineView) || (destination.ActiveWindow.View.Type == wdMasterView)) && (destination.Subdocuments.Count == 0))\r
158     // Change the Type property of the current document to normal\r
159     destination.ActiveWindow.View.Type = wdNormalView;\r
162 // Compare to the base document\r
163 if (parseInt(word.Version) <= vOffice2000)\r
165     // Compare for Office 2000 and earlier\r
166     try\r
167     {\r
168         destination.Compare(sBaseDoc);\r
169     }\r
170     catch(e)\r
171     {\r
172         WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);\r
173         // Quit\r
174         WScript.Quit(1);\r
175     }\r
177 else\r
179     // Compare for Office XP (2002) and later\r
180     try\r
181     {\r
182         destination.Compare(sBaseDoc, "Comparison", wdCompareTargetNew, true, true);\r
183     }\r
184     catch(e)\r
185     {\r
186         WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);\r
187         // Close the first document and quit\r
188         destination.Close(wdDoNotSaveChanges);\r
189         WScript.Quit(1);\r
190     }\r
192     \r
193 // Show the comparison result\r
194 if (parseInt(word.Version) < vOffice2007)\r
196         word.ActiveDocument.Windows(1).Visible = 1;\r
198     \r
199 // Mark the comparison document as saved to prevent the annoying\r
200 // "Save as" dialog from appearing.\r
201 word.ActiveDocument.Saved = 1;\r
202     \r
203 // Close the first document\r
204 if (parseInt(word.Version) >= vOffice2002)\r
206     destination.Close(wdDoNotSaveChanges);\r