【问题标题】:Get NSTextFieldCell value using AppleScript使用 AppleScript 获取 NSTextFieldCell 值
【发布时间】:2018-02-19 12:01:59
【问题描述】:

我正在尝试自动化一些需要在对话框中显示文本的任务(6 位代码)。

Accessibility Inspector 揭示了以下层次结构:

我是 AppleScript 领域的初学者,并且已经阅读了一些可以像这样完成类似操作的示例:

set myText to textField's stringValue() as text

但我不确定这是否适用于我的情况,因为 Accessibility Inspector 没有显示包含 6 位代码的 NSTextFieldCell 的任何变量名称。

如何提取 NSTextFieldCell 中的 6 位代码并可能返回此值以便 shell 脚本可以使用此代码?

我现在有这样的东西 -

tell application "FollowUpUI"
    activate
    # get the 6 digit code
end tell

更新

经过一些帮助,我尝试遍历文本字段

tell application "System Events"

    repeat with theProcess in processes

        #initialize
        tell theProcess
            set processName to name
            set allWindows to windows
        end tell

        #check if process exists
        if processName is "FollowUpUI" then

            activate

            say "FollowUpUI found"

            set windowsCount to count of the allWindows

            #only one window should exist
            if windowsCount is 1 then
                say "1 window was found"

                tell window 1
                    tell group 1
                        tell text field 1
                            set code to value
                        end tell
                    end tell
                end tell

            end if
        end if
    end repeat
end tell

但我因为错误而卡住了 -

System Events got an error: cant get window 1. Invalid index.

我不确定这是否是语法错误。任何指针都会有所帮助。谢谢

【问题讨论】:

  • 您必须在 AppleScript 中使用带有System Events 的(丑陋的)GUI 脚本。您无法使用 Cocoa 访问其他应用程序/进程的 UI 元素。
  • 您能否指出正确的方向,也许是一个例子?我不会介意丑陋的解决方案。
  • 你必须tell application "System Events" to tell process "[name of process]" to tell window [1] to tell group 1 to tell text field 1 to set theValue to its value。 [] 中的所有值都是任意的,并且是没有括号的真实代码。
  • 如果我正确理解了这一点,这可能涉及到一些“命中和试验”。我如何找到正确的值放入[]。我可以打印出windows 或groups 以找到正确的值吗?抱歉,我对此很陌生。
  • 它比 hit and trial 少一点烦人。首先,您需要获取进程的名称。应用程序的名称似乎是FollowUpUI,因此将名称与tell application "System Events" to get name of processes 的结果进行比较。它很可能是窗口 1(除非有另一个不可见的窗口)。 你必须使用System Events,你不能直接访问应用程序,它肯定是不可编写脚本的。

标签: objective-c macos user-interface cocoa applescript


【解决方案1】:

出现错误是因为你必须引用window 1 of process "FollowUpUI"

你的代码太复杂了,你只需要检查进程是否存在

tell application "System Events"
    if exists process "FollowUpUI" then
        tell process "FollowUpUI"
            tell window 1
                tell static text 1 of group 1
                    set code to its value
                end tell
            end tell
        end tell
    end if
end tell

如果代码是工作流的一部分,您必须等到窗口打开

tell application "System Events"
    repeat until exists window 1 of process "FollowUpUI"
        delay 0.2
    end repeat
    tell window 1 of process "FollowUpUI"
        tell static text 1 of group 1
            set code to its value
        end tell
    end tell
end tell

我在value 之前添加了its 以确保它引用当前引用(文本字段)

由于您不向窗口发送键或鼠标事件,因此您不需要activate 任何东西。

【讨论】:

  • 谢谢@vadian,我正在打开对话框进行测试。这两个 sn-ps 仍然抛出相同的错误 - System Events got an error: Cant get text field 1 of group 1 of window 1 of process "FollowUpUI". Invalid Index。我在告诉进程语句后提出了say "found" 语句来检查进程是否存在。再次感谢所有帮助。
  • 那么引用错误。您可以使用UI Browser 之类的工具(不是免费的,但可能有试用期)或Script Debugger 之类的编辑器(也不是免费的),它有一个属性检查器。或者您必须在受影响的窗口打开时从根(进程)到目标(文本字段)logUI elements 找出引用链。
  • PS:我怀疑它不是 文本字段,它是别的东西——正如 ...Cell 所揭示的那样
  • 是的,它不是文本字段。无障碍检查员称其为NSTextFieldCell。 applescript 是否支持 NSTextField?此外,如果我使用Script Debugger 或类似工具,它们会显示与辅助功能检查器不同的层次结构吗?
  • 例如UI Browser 可以为选定的引用提供实际的AppleScript 代码。提到的两个工具都可以提供有关 UI 元素的更多信息,例如类和属性。
【解决方案2】:

不是答案,但我无法在评论中粘贴代码。这是获取 UI 元素的技巧。按 Command-Shift-4 以获取带有坐标的光标。瞄准 UI 元素并记住坐标。单击以使箭头光标返回。使用此脚本中的坐标。

activate application "FollowUpUI"
tell application "System Events"
    tell application process "FollowUpUI"
        click at {290, 150}
    end tell
end tell

必须允许脚本编辑器控制计算机。您可以在系统偏好设置、安全和隐私、隐私中进行设置。

【讨论】:

  • 我需要先提取 6 位代码,然后才能点击完成键。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-21
  • 2015-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多