c4bea7cc84126ae8af75487a279fb85372c1f032
[TortoiseGit.git] / contrib / diff-scripts / merge-doc.js
blobc4bea7cc84126ae8af75487a279fb85372c1f032
1 // extensions: doc;docx;docm
2 //
3 // TortoiseSVN Merge script for Word Doc files
4 //
5 // Copyright (C) 2004-2008, 2011 the TortoiseSVN team
6 // This file is distributed under the same license as TortoiseSVN
7 //
8 // Last commit by:
9 // $Author$
10 // $Date$
11 // $Rev$
13 // Authors:
14 // Dan Sheridan, 2008
15 // Davide Orlandi and Hans-Emil Skogh, 2005
16 // Richard Horton, 2011
19 var objArgs, num, sTheirDoc, sMyDoc, sBaseDoc, sMergedDoc,
20     objScript, word, baseDoc, myDoc, theirDoc, WSHShell;
22 // Microsoft Office versions for Microsoft Windows OS
23 var vOffice2000 = 9;
24 var vOffice2002 = 10;
25 //var vOffice2003 = 11;
26 var vOffice2007 = 12;
27 var vOffice2010 = 14;
28 // WdCompareTarget
29 var wdCompareTargetSelected = 0;
30 //var wdCompareTargetCurrent = 1;
31 var wdCompareTargetNew = 2;
32 var wdMergeTargetCurrent = 1;
34 objArgs = WScript.Arguments;
35 num = objArgs.length;
36 if (num < 4)
38     WScript.Echo("Usage: [CScript | WScript] merge-doc.js merged.doc theirs.doc mine.doc base.doc");
39     WScript.Quit(1);
42 sMergedDoc = objArgs(0);
43 sTheirDoc = objArgs(1);
44 sMyDoc = objArgs(2);
45 sBaseDoc = objArgs(3);
47 objScript = new ActiveXObject("Scripting.FileSystemObject");
49 if (!objScript.FileExists(sTheirDoc))
51     WScript.Echo("File " + sTheirDoc + " does not exist.  Cannot compare the documents.", vbExclamation, "File not found");
52     WScript.Quit(1);
55 if (!objScript.FileExists(sMergedDoc))
57     WScript.Echo("File " + sMergedDoc + " does not exist.  Cannot compare the documents.", vbExclamation, "File not found");
58     WScript.Quit(1);
61 objScript = null;
63 try
65     word = WScript.CreateObject("Word.Application");
67 catch (e)
69     WScript.Echo("You must have Microsoft Word installed to perform this operation.");
70     WScript.Quit(1);
73 word.visible = true;
75 // Open the base document
76 baseDoc = word.Documents.Open(sTheirDoc);
78 // Merge into the "My" document
79 if (parseInt(word.Version) < vOffice2000)
81     baseDoc.Compare(sMergedDoc);
83 else if (parseInt(word.Version) < vOffice2007)
85     baseDoc.Compare(sMergedDoc, "Comparison", wdCompareTargetNew, true, true);
87 else if (parseInt(word.Version) < vOffice2010)
89     baseDoc.Merge(sMergedDoc);
91 else
93     //2010 - handle slightly differently as the basic merge isn't that good
94     //note this is designed specifically for svn 3 way merges, during the commit conflict resolution process
95     theirDoc = baseDoc;
96     baseDoc = word.Documents.Open(sBaseDoc);
97     myDoc = word.Documents.Open(sMyDoc);
99     baseDoc.Activate(); //required otherwise it compares the wrong docs !!!
100     baseDoc.Compare(sTheirDoc, "theirs", wdCompareTargetSelected, true, true);
102     baseDoc.Activate(); //required otherwise it compares the wrong docs !!!
103     baseDoc.Compare(sMyDoc, "mine", wdCompareTargetSelected, true, true);
105     //theirDoc.Save();
106     //myDoc.Save();
107     myDoc.Activate(); //required? just in case
108     myDoc.Merge(sTheirDoc, wdMergeTargetCurrent);
111 // Show the merge result
112 if (parseInt(word.Version) < vOffice2007)
114     word.ActiveDocument.Windows(1).Visible = 1;
117 // Close the first document
118 if ((parseInt(word.Version) >= vOffice2002) && (parseInt(word.Version) < vOffice2010))
120     baseDoc.Close();
123 // Show usage hint message
124 WSHShell = WScript.CreateObject("WScript.Shell");
125 if (WSHShell.Popup("You have to accept or reject the changes before\nsaving the document to prevent future problems.\n\nWould you like to see a help page on how to do this?", 0, "TSVN Word Merge", 4 + 64) === 6)
127     WSHShell.Run("http://office.microsoft.com/en-us/assistance/HP030823691033.aspx");