如何在不使用AppleScript打开的情况下获取弹出菜单中的项目数量?

时间:2012-11-16 11:47:55

标签: macos automation applescript

我想编写一个脚本,以几种格式保存图像。事情是基于某些条件显示格式。我的意思是有时会有5种格式,有时甚至是8.我希望完全自动化这些节省的工作。所以我决定写一个苹果。有UI浏览器并使用它我可以访问每个弹出菜单。我正在使用循环来执行保存操作。问题是我没有得到结束的地方。所以我想到如果我可以在弹出菜单中获得项目数量,那么我将很容易执行任务。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

这是可能的,但您不能直接计算菜单项。通信位于GUI端,而不是直接连接到应用程序,这意味着菜单需要在您计算之前显示。

tell application "System Events"
    tell process "Your application"
        --we need to menu to appear first
        click pop up button 1 of window 1
        --now the menu appeared we can count the items in it
        count menu items of menu 1 of pop up button 1 of window 1
        --now hide the menu again by pressing escape
        key code 53
    end tell
end tell

计数是检查菜单的一种方法,但另一种方法是获取其中的所有值,然后按其名称单击右侧菜单项。这可能不是你的情况,通常是最好的解决方案。

set menuItemToSelect to "Title of menu item I prefer to check"

tell application "System Events"
    tell process "Your Application"
        tell pop up button 1 of window 1
            --Only continue if the menu item isn't already selected
            if value of it is not equal to menuItemToSelect then
                --we need to menu to appear first
                click it
                --now the menu appeared we can get the items
                set menuItemTitles to name of menu items of menu 1
                --check if the menu item exists
                if menuItemToSelect is in menuItemTitles then
                    --menu item exists; click on it
                    click menu item menuItemToSelect of menu 1
                else
                    --the menu item to select doesn't exist; hide the menu
                    key code 53
                end if
            end if
        end tell
    end tell
end tell