- app.addMenuItem({
- cName: "从附件添加书签", cParent: "Tools",
- cExec: "bookm();",
- cEnable: "event.rc= (event.target != null);" });
- var Name_Bookmark;
- var Page_Bookmark;
- function bookm()
- {
- var StartNum = app.response({
- cQuestion: "输入页码为1的实际页数",
- cTitle: "若缺少1页码,则输入2页码的实际页数,依此类推",
- cDefault: "1",
- cLabel: "输入:"
- });
- if (StartNum!=null)
- {
- var myThis=event.target;
- var attName=myThis.dataObjects[0].name;
- var oFile = myThis.getDataObjectContents(attName);
- var arrFile = util.stringFromStream(oFile, "utf-8");
- var tArr=arrFile.split("\n");
- myThis.bookmarkRoot.remove();
- myThis.bookmarkRoot.createChild("目录");
- var fSpc,lSpc;
- var tCht;
- var oDot;
- var Num_Dot;
- var Bkm= new Array(10);
- Bkm[0]=myThis.bookmarkRoot.children[0];
- var i;
- var a;
- var b;//首空格后的字符
- for (i=0;i<tArr.length;i++)
- {
- a=tArr[i];
- if (a.length>2)//空行的length=1,设置为2即不添加空行
- {
- //fSpc=a.indexOf("\u0020");
- tCht=a.match(/^.+?\s/);
- oDot=tCht.toString().match(/\./g);//匹配.符号
- Num_Dot=(oDot==null? 0:oDot.length);
- lSpc=a.lastIndexOf("\u0020");//末空格的位置
- b=Number(a.slice(lSpc));
- Page_Bookmark=(isNaN(b)? 0:b-2+Number(StartNum));//书签指向的页码
- Name_Bookmark=(isNaN(b)? a:a.slice(0,lSpc));//书签的名字
- Bkm[Num_Dot+1]=CreateSubBkm(Bkm[Num_Dot]);
- }
- }
- }
- }
- function CreateSubBkm(RootBkm)//在RootBkm书签底下创建一个新书签,并返回新书签对象
- {
- var RootBkm;
- var subBookmark;
- with (RootBkm)
- {
- createChild(Name_Bookmark, "this.pageNum="+Page_Bookmark+";");
- insertChild(children[0],children.length);
- subBookmark=children[children.length-1];//目录中最后一个章节
- }
- return subBookmark;
- }
本文出自 “一些心得” 博客