Xcode Simulator.app中自动切换菜单设置的Automator脚本

Xcode Simulator.app中自动切换菜单设置的Automator脚本,xcode,applescript,ios-simulator,xcode-ui-testing,automator,Xcode,Applescript,Ios Simulator,Xcode Ui Testing,Automator,我需要为我的UITests自动注册Face ID和Touch ID。为此,我正在编写一个自动机脚本 我当前的automator脚本,目前可以在菜单中自动单击“已注册”: on run {input, parameters} if application "Simulator" is running then tell application "System Events" set theName to name of the first process whose fr

我需要为我的UITests自动注册Face ID和Touch ID。为此,我正在编写一个自动机脚本

我当前的automator脚本,目前可以在菜单中自动单击“已注册”:

on run {input, parameters}

if application "Simulator" is running then
    tell application "System Events"
        set theName to name of the first process whose frontmost is true
    end tell
    tell application "Simulator" to activate
    tell application "System Events"
        tell process "Simulator"
            tell menu bar 1
                tell menu bar item "Hardware"
                    tell menu "Hardware"
                        tell menu item "Face ID"
                            tell menu "Face ID"
                                click (menu item "Face ID" where its name starts with "Matching")
                            end tell
                        end tell
                    end tell
                end tell
            end tell
        end tell
    end tell
    tell application theName to activate
end if
return input
end run 
问题如下。我需要确定设备是否已注册。有一个复选标记,显示当前状态。我已经试着检查一下是否有复选标记。但我还没能让它起作用


所以我的问题是。我该怎么做,这样脚本只会在没有复选标记的情况下按下“已注册”菜单项?

菜单项的
属性名为
AXMenuItemMarkChar
,它要么设置为表示菜单项旁边复选标记的字符(如果选中),要么设置为
缺少值

    use application "System Events"

    tell application "Simulator" to activate

    tell process "Simulator" to tell menu bar 1 to ¬
        tell menu bar item "Hardware" to tell menu "Hardware" to ¬
            tell menu item "Face ID" to tell menu "Face ID" to ¬
                tell menu item "Enrolled"

                    if the value of the attribute "AXMenuItemMarkChar" is not "✓" then ¬
                        click it

                    delay 1 -- !important

                    return the value of the attribute "AXMenuItemMarkChar"

                end tell
在我的测试中,如果模拟器应用程序当时处于焦点位置,则该属性返回正确的值。此脚本的返回值应始终为“✓", 因为如果没有选中菜单项,脚本将继续单击它,并在其旁边打上复选标记;如果选中菜单项,则除了通过获取属性值来确认是否选中之外,无需做任何事情

脚本的一个问题是对不存在的菜单项的引用错误(
菜单项“Face ID”的菜单项“Face ID”的菜单项“Face ID”
),然后使用
where
子句对具有不同值的名称进行筛选;因此它将不返回值,并且您将没有可单击的菜单项

希望我的脚本能为你工作。如果你有任何问题,请告诉我



此答案中包含的AppleScript已在MacOS 10.13上进行了测试。这是一个示例脚本,用于说明如何满足您的目标。可能需要根据您的系统调整
delay
命令,并且该脚本可能会从一些错误处理中受益,以使其更加健壮。

有一个
菜单项的属性ed
AXMenuItemMarkChar
,设置为表示菜单项旁边复选标记的字符(如果选中),或
缺少值

    use application "System Events"

    tell application "Simulator" to activate

    tell process "Simulator" to tell menu bar 1 to ¬
        tell menu bar item "Hardware" to tell menu "Hardware" to ¬
            tell menu item "Face ID" to tell menu "Face ID" to ¬
                tell menu item "Enrolled"

                    if the value of the attribute "AXMenuItemMarkChar" is not "✓" then ¬
                        click it

                    delay 1 -- !important

                    return the value of the attribute "AXMenuItemMarkChar"

                end tell
在我的测试中,如果模拟器应用程序当时处于焦点,则该属性将返回正确的值。此脚本的返回值应始终为“✓,因为如果未选中菜单项,则脚本将继续单击该菜单项并在其旁边打上复选标记;如果选中菜单项,则除了通过获取属性值来确认该菜单项已被选中之外,无需其他操作

脚本的一个问题是对不存在的菜单项的引用错误(
菜单项“Face ID”的菜单项“Face ID”的菜单项“Face ID”
),然后使用
where
子句对具有不同值的名称进行筛选;因此它将不返回值,并且您将没有可单击的菜单项

希望我的脚本能为你工作。如果你有任何问题,请告诉我



此答案中包含的AppleScript已在MacOS 10.13上进行了测试。这是一个示例脚本,用于说明如何满足您的目标。
delay
命令可能需要根据您的系统进行调整,并且该脚本可能会从一些错误处理中受益,以使其更加健壮。

可能重复我已经看过的主题,b但是我还没能让它在我已经看过的主题的4kman可能的副本中工作,但是我还没能让它在4kman中工作,谢谢答案,还有很好的解释!但是如果“已注册”是不可更改的,我想切换它并运行脚本。我得到以下错误:变量已注册菜单项不是def抱歉!我的错误。该变量是我以前的脚本草稿中遗留下来的。我现在将为您更新它。感谢您发现它!谢谢您!我将批准您的答案:-)谢谢您的答案,以及很好的解释!但是如果“已注册”未更改,并且我希望切换它并运行脚本。我得到以下错误:variabl未定义e注册菜单项。抱歉!我的错误。该变量是我以前的脚本草稿中留下的。我现在将为您更新它。感谢您发现此问题!谢谢!我将批准您的答案:-)