(转)利用新增加的库 accObject将Word界面中内容取出来
By
admin
at 2020-12-24 • 0人收藏 • 1390人看过
感谢: 圣地(113xxxx18) 提供源码
将Word这个界面中内容取出来,本来想用 winex 来模似操作后发现这个界面用 spy++ 分析只有一个句柄
最后尝试了最新增加的库 accObject 效果很好,而且可以后台自动操作 你在正常使用电脑不影响自动化的操作
自动点击取到界面指定部件显示内容后成 csv 文件
import win.ui; /*DSG{{*/ var winform = win.form(text="aardio form";right=759;bottom=469) winform.add( button={cls="button";text="开始取(F5)";left=23;top=12;right=135;bottom=51;z=1}; button2={cls="button";text="test";left=573;top=9;right=701;bottom=41;z=3}; edit={cls="edit";left=16;top=66;right=742;bottom=455;edge=1;hscroll=1;multiline=1;vscroll=1;z=2} ) /*}}*/ import console; import winex; import winex.accObject; var hwnd //"自定义热键窗口"句柄 var accObject //"自定义热键窗口"对象 //取所有类别 var getCategory = function(accObject){ if(!accObject){ error("accObject为空",2); }; var accMessage = accObject.find(role="列表";name="类别\(C\)\:.*") if(!accMessage){ error("accMessage",2); } var ret={}; for accChild in accMessage.each(){ if(accChild.name() != "---------------------------------------------------"){ table.push(ret,accChild.name() ) } } return ret; } //取所有命令 var getCmd = function(accObject){ if(!accObject){ error("accObject为空",2); }; var accMessage = accObject.find(role="列表";name=".*?\(O|Y\)\:.*") if(!accMessage){ error("accMessage",2); } var ret={}; for accChild in accMessage.each(){ table.push( ret,accChild.name() ) } return ret; } //点击指定列表框条目 var clickItem = function(accObject,itemName){ if(!accObject){ error("accObject为空",2); } var accMessage = accObject.find(role="列表项目";name=itemName ) if(!accMessage){ error("accMessage为空",2); } accMessage.doDefaultAction() } //取到热键部分内容 var getHotkey = function(accObject){ if(!accObject){ error("accObject为空",2); } var accMessage = accObject.find(role="列表";name="当前快捷键\(U\)\:") if(accMessage){ var tab={}; for accChild in accMessage.each(){ table.push( tab,accChild.name() ) } return `"`+string.join(tab,';')+`"`; } } //取当前显示的命令说明 var getCommandDesc = function(accObject){ if(!accObject){ error("accObject为空",2); } var accMessage = accObject.find( name = "自定义键盘";role = "对话框" ) //取主窗口对象 var i=0; for accChild in accMessage.each(){ //这个部分没有明显特征只能根据位置找了(也就是分析器中的ID) i++; if(i==29){ return `"`+accChild.name()+`"` ; } } } var start = function(){ var str = "" hwnd = winex.findActivate("自定义键盘") var accObject = winex.accObject.fromWindow(hwnd) var arrCategory = getCategory(accObject) for(k,category in arrCategory){ //有一个名为「其他命令 | <<无标签>> 选项卡」含有特殊字符所以要用非模式语法 clickItem(accObject,"^<@"+ category +"@>$")//点击类别列表框 var arrCmd = getCmd(accObject) for(k,cmd in arrCmd){ clickItem(accObject,"^<@"+ cmd +"@>$")//点击命令列表框 winform.text = string.format("当前类别: %s 当前第 %s 个命令, 名称: %s",category,string.format("%.02d", k) ,cmd) //winform.edit.printf('%s %s %s %s',category,cmd,getHotkey(accObject):"", getCommandDesc(accObject):"") str = str + string.format( '%s,%s,%s,%s',category,cmd,getHotkey(accObject):"", getCommandDesc(accObject):"" ) +'\n' win.delay(100) } } string.save("\Word所有热键.txt", string.fromto(str,65001,936)) } winform.button.oncommand = function(id,event){ start() } hkid = winform.reghotkey( function(id,mod,vk){ winform.button.oncommand() } ,0,0x74/*_VK_F5*/ ); winform.button2.oncommand = function(id,event){ hwnd = winex.findActivate("自定义键盘") var accObject = winex.accObject.fromWindow(hwnd) console.log( getHotkey(accObject) ) console.log( getCommandDesc(accObject) ) } winform.show(); win.loopMessage();
登录后方可回帖