Honor renames in patch views
[TortoiseGit.git] / contrib / diff-scripts / diff-doc.js
blob7afd50fca29765f548ff0c08ca3e9a174d2cc237
1 // extensions: doc;docx;docm;dot;dotx;odt;ods\r
2 //\r
3 // TortoiseSVN Diff script for Word Doc files\r
4 //\r
5 // Copyright (C) 2004-2008, 2011, 2013, 2019 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
68     // disable macros\r
69     word.AutomationSecurity = 3; //msoAutomationSecurityForceDisable\r
71     if (parseInt(word.Version, 10) >= vOffice2013)\r
72     {\r
73         var f = objScript.GetFile(sBaseDoc);\r
74         if (f.attributes & 1)\r
75         {\r
76             f.attributes = f.attributes - 1;\r
77         }\r
78     }\r
79 }\r
80 catch (e)\r
81 {\r
82     // before giving up, try with OpenOffice\r
83     var OO;\r
84     try\r
85     {\r
86         OO = WScript.CreateObject("com.sun.star.ServiceManager");\r
87     }\r
88     catch (e)\r
89     {\r
90         WScript.Echo("You must have Microsoft Word or OpenOffice installed to perform this operation.");\r
91         WScript.Quit(1);\r
92     }\r
93     // yes, OO is installed - do the diff with that one instead\r
94     var objFile = objScript.GetFile(sNewDoc);\r
95     if ((objFile.Attributes & 1) === 1)\r
96     {\r
97         // reset the readonly attribute\r
98         objFile.Attributes = objFile.Attributes & (~1);\r
99     }\r
100     //Create the DesktopSet\r
101     var objDesktop = OO.createInstance("com.sun.star.frame.Desktop");\r
102     var objUriTranslator = OO.createInstance("com.sun.star.uri.ExternalUriReferenceTranslator");\r
103     //Adjust the paths for OO\r
104     sBaseDoc = sBaseDoc.replace(/\\/g, "/");\r
105     sBaseDoc = sBaseDoc.replace(/:/g, "|");\r
106     sBaseDoc = sBaseDoc.replace(/ /g, "%20");\r
107     sBaseDoc = sBaseDoc.replace(/#/g, "%23");\r
108     sBaseDoc = "file:///" + sBaseDoc;\r
109     sBaseDoc = objUriTranslator.translateToInternal(sBaseDoc);\r
110     sNewDoc = sNewDoc.replace(/\\/g, "/");\r
111     sNewDoc = sNewDoc.replace(/:/g, "|");\r
112     sNewDoc = sNewDoc.replace(/ /g, "%20");\r
113     sNewDoc = sNewDoc.replace(/#/g, "%23");\r
114     sNewDoc = "file:///" + sNewDoc;\r
115     sNewDoc = objUriTranslator.translateToInternal(sNewDoc);\r
117     //Open the %base document\r
118     var oPropertyValue = [];\r
119     oPropertyValue[0] = OO.Bridge_GetStruct("com.sun.star.beans.PropertyValue");\r
120     oPropertyValue[0].Name = "ShowTrackedChanges";\r
121     oPropertyValue[0].Value = true;\r
122     // objDocument is needed\r
123     var objDocument = objDesktop.loadComponentFromURL(sNewDoc,"_blank", 0, oPropertyValue);\r
125     //Set the frame\r
126     var Frame = objDesktop.getCurrentFrame();\r
128     var dispatcher = OO.CreateInstance("com.sun.star.frame.DispatchHelper");\r
130     //Execute the comparison\r
131     dispatcher.executeDispatch(Frame, ".uno:ShowTrackedChanges", "", 0, oPropertyValue);\r
132     oPropertyValue[0].Name = "URL";\r
133     oPropertyValue[0].Value = sBaseDoc;\r
134     dispatcher.executeDispatch(Frame, ".uno:CompareDocuments", "", 0, oPropertyValue);\r
135     WScript.Quit(0);\r
138 if (parseInt(word.Version, 10) >= vOffice2007)\r
140     sTempDoc = sNewDoc;\r
141     sNewDoc = sBaseDoc;\r
142     sBaseDoc = sTempDoc;\r
145 objScript = null;\r
146 word.visible = true;\r
148 // Open the new document\r
149 try\r
151     destination = word.Documents.Open(sNewDoc, true, (parseInt(word.Version, 10) < vOffice2013));\r
153 catch (e)\r
155     try\r
156     {\r
157         // open empty document to prevent bug where first Open() call fails\r
158         word.Documents.Add();\r
159         destination = word.Documents.Open(sNewDoc, true, (parseInt(word.Version, 10) < vOffice2013));\r
160     }\r
161     catch (e)\r
162     {\r
163         WScript.Echo("Error opening " + sNewDoc);\r
164         // Quit\r
165         WScript.Quit(1);\r
166     }\r
169 // If the Type property returns either wdOutlineView or wdMasterView and the Count property returns zero, the current document is an outline.\r
170 if ((destination.ActiveWindow.View.Type === wdOutlineView) || ((destination.ActiveWindow.View.Type === wdMasterView) || (destination.ActiveWindow.View.Type === wdReadingView)) && (destination.Subdocuments.Count === 0))\r
172     // Change the Type property of the current document to normal\r
173     destination.ActiveWindow.View.Type = wdNormalView;\r
176 // Compare to the base document\r
177 if (parseInt(word.Version, 10) <= vOffice2000)\r
179     // Compare for Office 2000 and earlier\r
180     try\r
181     {\r
182         destination.Compare(sBaseDoc);\r
183     }\r
184     catch (e)\r
185     {\r
186         WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);\r
187         // Quit\r
188         WScript.Quit(1);\r
189     }\r
191 else\r
193     // Compare for Office XP (2002) and later\r
194     try\r
195     {\r
196         destination.Compare(sBaseDoc, "Comparison", wdCompareTargetNew, true, true);\r
197     }\r
198     catch (e)\r
199     {\r
200         WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);\r
201         // Close the first document and quit\r
202         destination.Close(wdDoNotSaveChanges);\r
203         WScript.Quit(1);\r
204     }\r
207 // Show the comparison result\r
208 if (parseInt(word.Version, 10) < vOffice2007)\r
210     word.ActiveDocument.Windows(1).Visible = 1;\r
213 // Mark the comparison document as saved to prevent the annoying\r
214 // "Save as" dialog from appearing.\r
215 word.ActiveDocument.Saved = 1;\r
217 // Close the first document\r
218 if (parseInt(word.Version, 10) >= vOffice2002)\r
220     destination.Close(wdDoNotSaveChanges);\r