【问题标题】:AppleScript Not Recognizing Application Index AddressesAppleScript 无法识别应用程序索引地址
【发布时间】:2019-07-02 01:22:15
【问题描述】:

我正在创建一个 AppleScript 来控制 SAP 中的一些基本标准操作,并且从可用事务的主菜单页面中编写的所有内容都可以正常工作。当前脚本填写命令文本字段并打开一个新的现有事务代码。但是,一旦 SAP 进入新事务代码页,当我第二次运行 AppleScript 时,它不再识别命令文本字段的索引。相反,它将事务代码粘贴到恰好是当前焦点的任何文本字段中。当我运行脚本时,它应该总是转到相同的文本字段,因为整体索引地址没有改变,但事实并非如此。

我试图打开 SAP 的脚本字典,但我公司的 SAP 版本似乎禁用了脚本字典,所以我不得不使用索引地址来填充文本字段并单击按钮

on chooseSAPTransaction()
    global sapTransaction, transactionCode
    set transactionCode to "" -- intialize the variable
    set transactionList to choose from list {"Find Document", "Change Document", "Look up Product", "Correct Product"} with prompt "SAP Action:" default items {""}
    if transactionList is false then
        error number -128
    else
        set choosenTransaction to item 1 of transactionList
    end if
    if choosenTransaction is "Find Document" then
        set transactionCode to "code1"
        chooseCode1()
    end if
    if choosenTransaction is "Change Document" then
        set transactionCode to "code2"
        chooseCode2()
    end if
    if choosenTransaction is "Look up Product" then
        set transactionCode to "code3"
        chooseCode3()
    end if
    if choosenTransaction is "Correct Product" then
        set transactionCode to "code4"
        chooseCode4()
    end if
    set sapTransaction to "/n" & transactionCode
    activate application "SAP"
    delay 0.1
    tell application "System Events"
        tell process "SAP"
            tell front window to tell toolbar 1 to tell text field 1
                keystroke sapTransaction
            end tell
            tell front window to tell toolbar 1 to tell button 1 to perform action "AXPress"
        end tell
    end tell
end chooseSAPTransaction

在随后的脚本执行中,它会跳过 tell front window to tell toolbar 1 to tell text field 1 并直接转到 keystroke sapTransaction

"/n" & transactionCode 创建一个字符串,告诉 SAP 新事务要转到。手动键入时,它总是将您带到新事务,无论您在当前事务中可能有多少层)

我希望索引指令在运行脚本的每个新实例上都转到相同的元素,但是一旦运行一次,AppleScript 就不再找到元素地址了。

编辑:

我只是尝试将粘贴操作移动到它自己的唯一处理程序中,以便在尝试进入下一步之前可以调用它,但这也不起作用。

--剪辑--

    if choosenTransaction is "Find Document" then
        set transactionCode to "code1"
        set sapTransaction to "/n" & transactionCode
        pasteTransaction()
        chooseCode1()
    end if

--剪辑--

on pasteTransaction()
    global serialNumber, sapTransaction, transactionCode
    activate application "SAPGUI"
    delay 0.1
    tell application "System Events"
        tell process "SAPGUI"
            tell front window to tell toolbar 1 to tell text field 1
                keystroke sapTransaction
            end tell
            tell front window to tell toolbar 1 to tell button 1 to perform action "AXPress"
        end tell
    end tell
end pasteTransaction

编辑 2:

我通过强制 SAP 浏览菜单使其工作。哇!

on pasteTransaction()
    global serialNumber, sapTransaction, transactionCode
    activate application "SAPGUI"
    delay 0.1
    tell application "System Events"
        click menu item ¬
            "Target Command Field" of menu 1 of menu bar item ¬
            "Edit" of menu bar 1 of application process "SAPGUI"
        keystroke sapTransaction
        tell process "SAPGUI"
            tell front window to tell toolbar 1 to tell button 1 to perform action "AXPress"
        end tell
    end tell
end pasteTransaction

不幸的是,这并不能解决 AppleScript 没有为文本字段找到正确索引地址的更大问题,因为让这部分工作只是第一步。现在我必须将文本字符串放入每个事务中的各种文本字段中。

因此,如果有人知道如何解决原始索引地址问题,那就太好了。

我希望我知道如何实际编写 AppleScript,而不是一次将来自不同来源的一个动作拼凑在一起。它并不优雅,而且功能很简单。

【问题讨论】:

  • 我需要弄清楚如何让这些索引地址正常工作。我开始使用下一个处理程序,唯一能让它到达正确文本字段的方法是通过关键代码选项卡,这不仅笨拙,而且如果您的 SAP 副本与服务器的通信速度比我的慢,它也会中断。如果没有以相同的速度连接,则光标最终会出现在错误的字段中并单击错误的单选按钮。呕吐。

标签: applescript


【解决方案1】:

GUI 脚本始终是一种黑客行为,如果不查看应用程序的实际运行情况,就很难诊断问题。显然,界面中的某些东西正在改变你试图利用的视图层次结构,但是......我会指出菜单和工具栏往往是动态的,这使得事情变得更加复杂。但您可以尝试以下几种诊断方法和替代方法。

首先,让我像这样重写 GUI 代码,我觉得这样更清楚:

tell application "System Events"
    tell process "SAP"
        tell front window
            tell toolbar 1
                tell text field 1
                    keystroke sapTransaction
                end tell
                tell button 1
                    perform action "AXPress"
                end tell
            end tell
        end tell
    end tell
end tell

所以首先——假设你的目标是正确的文本字段——你可以试试这个:

            tell toolbar 1
                tell text field 1
                    set value to sapTransaction
                end tell
                tell button 1
                    perform action "AXPress"
                end tell
            end tell

keystroke 可以被任何具有焦点的 UI 元素拦截,但设置文本字段的 value 应该直接进入正确的元素。如果这不起作用,您可以在脚本编辑器中尝试此诊断:

            tell toolbar 1
                properties of every text field
            end tell

这应该会在脚本编辑器的日志中为您提供工具栏 1 中所有文本字段的列表。这样,您可以查看文本字段是否具有名称属性(这将使寻址更容易:即 tell text field "someName") ,或者如果某些其他文本字段已添加到层次结构中较高的工具栏,则更改您所追求的文本字段的索引。如果情况变得更糟,您可以试试这个:

            tell toolbar 1
                entire contents
            end tell

这将列出工具栏的所有 UI 元素。您可以进行之前/之后的比较,看看有什么(如果有的话)发生了变化。

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-25
    • 1970-01-01
    • 2017-07-28
    • 2021-04-07
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多