Javascript实例教程(20-10)

HoTMetal中使用Javascript

5.怎样编写脚本来检查上次修改的日期

这个On_Document_Activate宏是检查磁盘上的文件是否有与利用HoTMetaL编辑的当前文档相同的上次修改的日期。它提示用户该做什么以防日期不匹配。以下是这个宏的具体代码:

 1<macro desc="Runs Macro: Hide_On_Document_Activate" id="44" lang="JScript" name="On_Document_Activate" tooltip="Hide_On_Document_Activate"><![CDATA[   
 2  
 3// Do this for local documents only   
 4  
 5if (ActiveDocument.FullName == ActiveDocument.LocalFullName) {   
 6  
 7var name = ActiveDocument.LocalFullName;   
 8  
 9if (Application.ReadableFileExists(name)) { // if document has never been saved, do nothing   
10  
11var fso = new ActiveXObject("Scripting.FileSystemObject");   
12  
13var f = fso.GetFile(name);   
14  
15var newMod = Date.parse(f.DateLastModified);   
16  
17var props = ActiveDocument.CustomDocumentProperties;   
18  
19if (props.count != 0) {   
20  
21oldMod = props.Item("LastMod").value;   
22  
23if (oldMod != newMod) {   
24  
25var Yes = 6;   
26  
27var No = 7;   
28  
29var msg = "The disk version of this document has changed from the\n";   
30  
31msg += "version in memory. Do you want to re-open the document?";   
32  
33var ret = Application.MessageBox(msg, 36, "Document Changed");   
34  
35if (ret == Yes) {   
36  
37ActiveDocument.Reload();   
38  
39}   
40  
41// Reset the timestamp regardless of the user's response   
42  
43// This will prevent the dialog from always showing   
44  
45Application.Run("On_Document_Open_Complete");   
46  
47}   
48  
49}   
50  
51}   
52  
53}   
54  
55]]></macro>

我们再检查文件是否装载了: ActiveDocument.FullName == ActiveDocument.LocalFullName。然后我们验证一下文件是否被保存到磁盘中: Application.ReadableFileExists(name). 类似于前面的On_Document_Open_Complete 宏,我们创建一个ActiveX控件并且提取出文件的上次修改的日期,代码如下:

var fso = new ActiveXObject("Scripting.FileSystemObject");

var f = fso.GetFile(name);

var newMod = Date.parse(f.DateLastModified);

Published At
Categories with 网页设计
comments powered by Disqus