我already described 如何使用“对象大小和位置”对话框手动对齐字段。我现在编写了一个Sikuli 脚本来自动化对话框的使用。我成功地使用该脚本在现有堆叠字段下方堆叠了大约 70 个新字段。
要使用此脚本,请将其内容复制并粘贴到 Sikuli IDE 中。打开 Crystal Reports 并找到现有堆栈底部的字段。确保要添加到堆栈底部的新字段存在并且在屏幕上可见。选择堆栈底部的字段。然后切换到 Sikuli 并按 CtrlR 启动脚本。它将切换到 Crystal Reports 并打开“大小和位置”对话框,读取堆栈底部字段的现有值,然后关闭对话框。您现在有 1.5 秒(可配置)通过单击来选择新字段。现在脚本将再次打开“大小和位置”对话框并设置 X、Y、宽度和高度,以便将所选字段放置在前一个字段的下方。具体来说,X、Width 和 Height 设置为与上述字段相同,Y 设置为我在my other answer 中描述的那样。如果需要,您可以更改配置变量 VERTICAL_SPACE_BETWEEN_FIELDS 以在每个字段之间添加空格或使它们重叠。
脚本需要大约 5 秒来对齐一个字段。如果这太慢了,您可以尝试减少或删除一些wait() 调用的时间。我添加了 wait() 调用,因为如果脚本运行得太快,有时会复制或粘贴错误的值。
这个脚本的一个好处是它是可链接的。完成后,新对齐的字段仍将被选中。因此,如果您要在该字段下添加另一个字段,您只需使用 CtrlR 再次运行脚本,然后准备单击下一个字段中间。如果您计划多次链接,您可以在range(1) 中增加1,并在mainAction() 下方添加wait(<num_of_seconds>),以便脚本自动重复。请记住,链接要求要添加的下一个字段在屏幕上可见,因此您可以使用鼠标选择它。
我将脚本文件保存为“对齐 Crystal Reports.sikuli 中的字段”。
# Crystal Reports: stack prompt-selected field under start-selected field
VERTICAL_SPACE_BETWEEN_FIELDS = 0.000
WAIT_TIME_FOR_USER_SELECT_NEW_FIELD = 1.5
def mainAction():
# read size and position of bottom of stack
above = dict()
openSizeAndPositionDialog()
above['x'] = copySelectedText()
moveToNextField(2)
above['y'] = copySelectedText()
moveToNextField()
above['width'] = copySelectedText()
moveToNextField(2)
above['height'] = copySelectedText()
print("above", above)
wait(0.05)
type(Key.ESC)
# calculate size and position of next field in stack
new_field = dict()
new_field['x'] = above['x']
new_field['y'] = str(float(above['y']) + float(above['height']) + VERTICAL_SPACE_BETWEEN_FIELDS)
new_field['width'] = above['width']
new_field['height'] = above['height']
print("new field", new_field)
waitForUserToSelectNewField()
# set size and position of next field
openSizeAndPositionDialog()
paste(new_field['x'])
moveToNextField(2)
paste(new_field['y'])
moveToNextField()
paste(new_field['width'])
moveToNextField(2)
paste(new_field['height'])
wait(0.1)
type(Key.ENTER)
def openSizeAndPositionDialog():
type(Key.ALT + "a" + "z")
wait(0.05)
def copySelectedText():
type("c", KeyModifier.CTRL)
wait(0.05)
return Env.getClipboard()
def moveToNextField(numTimes=1):
for i in range(numTimes):
type(Key.TAB)
wait(0.05)
def waitForUserToSelectNewField():
# I'll do it without the popup, because switching to the popup and then closing it is a pain
wait(WAIT_TIME_FOR_USER_SELECT_NEW_FIELD)
#popup("select the new field to align under the old one, then press OK")
#wait(0.2)
App.focus("Crystal Reports")
wait(0.2) # give you time to release CTRL, which would interfere with the script
for i in range(1):
mainAction()