服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

javascript实例教程(19) 使用hotmetal(5)

hotmetal中使用javascript

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

在本节教程中你将可以学到怎样编写一个宏来检查是否有任何的程序已经利用hotmetal中修改过一个文件。这个宏包括了以下几个检查的更新特性:on_document_open_complete、on_document_activate和 on_application_activate。在前面的教程中,这些宏的名字已经被预定义了,所以这里不能对它们进行修改。这些名字指定了事件来触发宏。这个event-macro关联是隐含的,所以不能通过任何手段来对它进行改写。当我们打开一个文档的时候,比如on_document_open_complete,它总是在完成文件打开的时候被调用的。以下是具体的定义: <macro name="on_document_open_complete" lang="jscript"><![cdata[

var name = activedocument.localfullname;

if (application.readablefileexists(name)) { // if document has never been saved, do nothing

application.run("on_document_save");

}

]]></macro>

我们首先提取当前文件夹的文件名:name = activedocument.localfullname,然后检查可读的文件是否存在;接着我们运行宏on_document_save,这个宏on_document_save示范了微软的filesystemobject作为activex控件的使用方法,这是一个在javascript中。这个宏的主要思想是更新文档的lastmod属性以反应磁盘上文档的当前事件:

<macro name="on_document_save" lang="jscript"<>![cdata[

var fso = new activexobject("scripting.filesystemobject");

var f = fso.getfile(activedocument.localfullname);

var mod = date.parse(f.datelastmodified);

var props = activedocument.customdocumentproperties;

if (props.count != 0) {

props.add("lastmod", mod);

}

]]></macro>

这个宏从filesystemobject创建了一个activex控件,它包括了微软的脚本库: var fso = new activexobject("scripting.filesystemobject");

我们可以通过以下的语句来从磁盘得到文件的属性:f = fso.getfile(name),然后提取出文件最后一次修改的事件:mod = date.parse(f.datelastmodified)。我们通过调用activedocument的customdocumentproperties 属性来创建了一个用户定义的属性集:props。然后我们利用mod属性来对这个集进行初始化,这时它的数值为"lastmode"。

hotmetal中使用javascript

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

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

<macro name="on_document_activate" lang="jscript" id="44" tooltip="hide_on_document_activate"

desc="runs macro: hide_on_document_activate"><![cdata[

// do this for local documents only

if (activedocument.fullname == activedocument.localfullname) {

var name = activedocument.localfullname;

if (application.readablefileexists(name)) { // if document has never been saved, do nothing

var fso = new activexobject("scripting.filesystemobject");

var f = fso.getfile(name);

var newmod = date.parse(f.datelastmodified);

var props = activedocument.customdocumentproperties;

if (props.count != 0) {

oldmod = props.item("lastmod").value;

if (oldmod != newmod) {

var yes = 6;

var no = 7;

var msg = "the disk version of this document has changed from the/n";

msg += "version in memory. do you want to re-open the document?";

var ret = application.messagebox(msg, 36, "document changed");

if (ret == yes) {

activedocument.reload();

}

// reset the timestamp regardless of the user's response

// this will prevent the dialog from always showing

application.run("on_document_open_complete");

}

}

}

}

]]></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);
hotmetal中使用javascript

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

接着,我们调用当前文档的定制属性集:props = activedocument.customdocumentproperties 并且检查这个属性的数字是否不等于零。我们已经在前面的on_document_open_complete 宏中已经保存了,并将它赋值给oldmod:

oldmod = props.item("lastmod").value

当我们发现oldmod (来自打开的文档) and newmod (来自磁盘)之间的矛盾的时候,我们应该告诉用户是否从磁盘上转载了这个文件:

var yes = 6;

var no = 7;

var msg = "the disk version of this document has changed from the/n";

msg += "version in memory. do you want to re-open the document?";

var ret = application.messagebox(msg, 36, "document changed");

if (ret == yes) {

activedocument.reload();

}

最后,我们通过模仿打开的操作来重置当前文档的日期:

application.run("on_document_open_complete");

我们想扩展这个更新特性的检查并触发它,而不管在这个文档是当前的还是当这个应用程序是当前的。这时我们可以定义on_application_activate宏,这个宏只是调用上面的宏:

<macro name="on_application_activate" lang="jscript"><![cdata[

application.run("on_document_activate");

]]></macro>

现在我们需要复制on_document_save功能到on_document_saveas宏:

<macro name="on_document_saveas" lang="jscript"<>![cdata[

application.run("on_document_save");

]]></macro>

最后还是对它进行一下测试吧。先在hotmetal pro 6.0中打开一个文档。并在你喜欢的编辑器中打开相同的文档。并在任何地方插入一个空格符再将它保存到磁盘中。当你切换到hotmetal应用程序,你将可以得到如图1的信息。

javascript实例教程(19) 使用hotmetal(5)

(图1)

扫描关注微信公众号