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