如何从终端控制图形程序?

如何从终端控制图形程序?

我正在尝试从终端控制 Inkscape 程序;例如,我想从终端打开 Inkscape 中的文件菜单、编辑菜单或扩展菜单,而无需从程序本身单击菜单。

我搜索了很多,但还是找不到问题的答案。如何从终端控制图形程序?

答案1

我很确定LDTP 协议可以帮助您自动化 Inkscape GUI。首先打开终端并安装 python 绑定:

sudo apt-get install python-ldtp

然后启动python解释器:

$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ldtp import *
>>> launchapp('inkscape')
25362
>>> GTK Accessibility Module initialized
>>> selectmenuitem('*Inkscape', 'mnuExtensions')

或者使用以下代码片段创建专用脚本:

#!/usr/bin/env python2

from ldtp import *
import time

launchapp('inkscape')
waittillguiexist('*-Inkscape')
selectmenuitem('*-Inkscape', 'mnuExtensions')
time.sleep(2)
selectmenuitem('*-Inkscape', 'mnuEdit')
time.sleep(2)
selectmenuitem('*-Inkscape', 'mnuFile')
time.sleep(2)
selectmenuitem('*-Inkscape', 'mnuQuit')
waittillguinotexist('*-Inkscape')

了解所有 GUI 组件名称有点困难,因为ldtpeditor它已被弃用(请参阅freedesktop 页面freedesktop-ldtp-dev 邮件列表中的此主题)。

要确定要使用什么控件来自动化应用程序,最好的办法就是依靠getobjectlist)('<window name>')

例子(设置重新堆叠水平点和实时预览):

在此处输入图片描述

$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ldtp import *
>>> launchapp('inkscape')
11413
>>> GTK Accessibility Module initialized

>>> selectmenuitem('*Inkscape', 'Restack...')
1
>>> getobjectlist('Restack')
['dlgRestack', 'flr8', 'flr9', 'flr4', 'flr5', 'lblHorizontalPoint', 'flr7', 'flr0',
'flr1', 'flr2', 'flr3', 'lblVerticalPoint', 'mnuBottomtoTop(90)', 'mnuTop', 'spr1', 
'mnu8', 'flr11', 'mnu0', 'cboLefttoRight(0)', 'mnuRight', 'mnuRadialInward', 
'mnuMiddle1', 'mnu12', 'lblLivepreview', 'cboTop', 'flr6', 'mnuLefttoRight(0)', 
'btnApply', 'lblAngle', 'chk0', 'mnuRighttoLeft(180)', 'mnuRadialOutward', 'sbtn0', 
'mnuArbitraryAngle', 'lblRestackDirection', 'mnuLeft', 'btnClose', 'mnuMiddle', 
'mnuBottom', 'flr12', 'flr10', 'spr0', 'mnuToptoBottom(270)', 'cboMiddle']
>>> click('dlgRestack', 'mnuMiddle1')
1
>>> click('dlgRestack', 'mnuMiddle')
1
>>> click('dlgRestack', 'chk0')
1
>>> click('dlgRestack', 'btnApply')
1
>>> click('dlgRestack', 'chk0')
1
>>> click('dlgRestack', 'btnApply') 

相关内容