EmEditor脚本优化:能否直接扩展选择范围提取拼写错误单词?
直接提取EmEditor中所有拼写错误单词的脚本优化
我希望提取EmEditor中所有拼写错误的单词。当前使用如下脚本,先通过特殊字符串“@@@”标记所有拼写错误单词,再借助该标记完成单词提取。我想了解是否可以直接扩展选择范围以包含多个独立单词,若可行则无需使用“@@@”标记。
原脚本
document.selection.StartOfDocument(false); while (true) { var xPos = document.selection.GetActivePointX(eePosView) var yPos = document.selection.GetActivePointY(eePosView) editor.ExecuteCommandByID(4554); // Next Misspelling var xPos2 = document.selection.GetActivePointX(eePosView) var yPos2 = document.selection.GetActivePointY(eePosView) if (xPos === xPos2 && yPos === yPos2) break document.selection.text = "@@@" } document.selection.Find("@@@[[:word:]]+", eeFindNext | eeFindReplaceRegExp | eeFindExtract | eeFindMatchedOnly, eeExFindSeparateCRLF | eeExFindRegexOnigmo) document.selection.Replace("@@@", "", eeReplaceAll, 0)
优化方案:直接扩展选择范围收集错误单词
完全可以不用标记字符串,直接通过多选功能一次性选中所有拼写错误的单词,再统一提取。改进后的脚本逻辑更简洁,也避免了修改原文档的操作:
document.selection.StartOfDocument(false); document.selection.Clear(); // 清空初始选择范围 while (true) { // 记录当前逻辑位置,用于判断是否遍历完成 var startX = document.selection.GetActivePointX(eePosLogical); var startY = document.selection.GetActivePointY(eePosLogical); // 跳转到下一个拼写错误单词 editor.ExecuteCommandByID(4554); // Next Misspelling var endX = document.selection.GetActivePointX(eePosLogical); var endY = document.selection.GetActivePointY(eePosLogical); // 位置未变化,说明已无更多拼写错误,退出循环 if (startX === endX && startY === endY) { break; } // 将当前错误单词加入多选集合 document.selection.AddSelection(); } // 提取所有选中的错误单词,这里以复制到剪贴板和新建文档为例 var misspelledWords = document.selection.Text; editor.Copy(); // 复制到剪贴板 editor.NewFile(); document.selection.Text = misspelledWords; // 粘贴到新文档显示
脚本说明
- 开头清空选择范围,确保从无选中状态开始遍历
- 每次定位到错误单词后,用
AddSelection()保留之前的选中内容并新增当前单词,实现多单词同时选中 - 遍历结束后,直接通过
document.selection.Text获取所有错误单词的内容,后续可根据需求处理(比如复制、保存到文件等) - 改用
eePosLogical而非eePosView判断位置,避免窗口缩放、换行显示导致的位置判断误差
内容的提问来源于stack exchange,提问作者user17727069
相关产品推荐
相关产品推荐

