November 24, 2007
Here's another post mainly written to show you why WebCoder scripting rules :). A customer complained about the lack of the "Auto update date" feature, which was present in WebCoder 2005, as well as a long range of versions before that. I didn't think anyone was really using it, which is why it was skipped for WebCoder 2007. Now that I know that at least one person is still using it, re-implementing it using WebCoder scripting seemed like an interesting idea. And as it turned out, it was pretty easy :). Here is how you do it.
First of all, we need a way to insert the special "tag" that allows WebCoder to update the timestamp in the file. A nice approach would be to add it to the Tools part of WebCoder, but that would be a bit harder to explain (although pretty easy to carry out). Instead, simply create a User button in WebCoder, and make it execute the following simple piece of scripting code:
from System import DateTime ScriptUtils.InsertCode("<!--WebCoderAutoDate-->Last updated " + DateTime.Now.ToString("MM-dd-yyyy HH:mm") + "<!--/WebCoderAutoDate-->")
Once the button is clicked, WebCoder will insert the special tag. The comments simply mark the text, to allow for easy recognition. You may change both the text as well as the format string used on the ToString method, but if you do, please remember to change it in the next part as well.
Now, we need to make sure that WebCoder changes the date automatically when saving the document, so in other words, we need to know when the Save button is clicked. No problem, let's hook into the BarManager and listen for the CommandClick event - by doing so, we can monitor every single toolbar and menu item in WebCoder, and wait for the user to click the ones we're interested in. The code below should be saved as a script (select Scripting -> Create new script, paste it in and then select Save), and then you should set it to start up along with WebCoder - otherwise, it won't update your dates until the script has been manually executed. After you have saved the script, select Scripting -> Reload script menu, then select Scripting -> Manage scripts, find your script and check the auto start checkbox.
clr.AddReferenceByPartialName("ActiproSoftware.SyntaxEditor.Net20") from System.Text.RegularExpressions import Regex from System import DateTime from ActiproSoftware.SyntaxEditor import DocumentModificationType, LineTerminator, DocumentModificationOptions from TSW.WebCoder.Classes import DocumentController def CommandClickHandler(s, e): cmd = e.Command.FullName if((cmd == "Files.SaveDocument") or (cmd == "Files.SaveDocumentSplitButton")): regex = Regex("Last updated ([0-9-/:. ]*)"); m = regex.Match(MainForm.Editor.Document.GetText(LineTerminator.CarriageReturn)) if(m.Success): MainForm.Editor.Document.ReplaceText(DocumentModificationType.Replace, m.Index, m.Length, "Last updated " + DateTime.Now.ToString("MM-dd-yyyy HH:mm") + "", DocumentModificationOptions.RetainSelection) MainForm.SaveEditorFile(DocumentController.GetInstance().ActiveDocument.Filename) DocumentController.GetInstance().ActiveDocument.Modified = False Editor.Document.Modified = False MainForm.BarManager.CommandClick += CommandClickHandler
And that's it - the auto update date functionality is back, courtesy of WebCoder scripting. You gotta love that :)