AppleScript - 如何搜索字符串的结果?

AppleScript - 如何搜索字符串的结果?

我正在尝试搜索 AppleScript 的结果以确定是否出现字符串。

运行此代码:

tell application "System Events" to tell process "Box Sync" to ¬
    tell menu bar item 1 of menu bar 2
        click
        get menu items of menu 1
        set myStatus to menu items of menu 1
        set myResult to result
        return myResult             
    end tell

结果是:

{menu item "Files Synced" of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item 2 of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item "Pause" of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item 4 of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item "Open Box Sync Folder" of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item "Open Box.com" of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item 7 of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item "Preferences…" of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item 9 of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events", menu item "Quit" of menu 1 of menu bar item 1 of menu bar 2 of application process "Box Sync" of application "System Events"}

现在我想搜索这个结果,看看是否存在“文件已同步”。但是运行

 myResult contains "Files Synced"

再次给我打印出整个结果。我如何搜索这个结果来确定是否存在字符串?

答案1

return myResult两次都得到了相同的打印输出,因为您在第一次运行后 没有删除该行。return当脚本到达此命令时将始终终止脚本。

▸ 另外,更改此项:

    set myStatus to menu items of menu 1

更改为:

    set myResult to name of menu items of menu 1

▸ 删除此行:

    get menu items of menu 1

还有这一行:

    set myResult to result

(他们实际上什么也没做。)

您的最终脚本将如下所示:

    tell application "System Events" to tell process "Box Sync" to ¬
        tell menu bar item 1 of menu bar 2
            click
            set myResult to name of menu items of menu 1
            myResult contains "Files Synced"
        end tell

将返回truefalse

或者,不使用显式变量声明(并使用 AppleScript 预定义result变量):

    tell application "System Events" to tell process "Box Sync" to ¬
        tell menu bar item 1 of menu bar 2
            click
            get the name of menu items of menu 1
            result contains "Files Synced"
        end tell

如果您需要任何说明或有任何其他疑问,请随时发表评论,我会回复您。如果它有助于解决您的问题,请考虑选择它作为您接受的答案。

相关内容