OSDN Git Service

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