iTunes 中的本地化菜单栏项字符串



以下代码将在iTunes中禁用随机播放模式:

tell application "System Events"
    tell process "iTunes"
        click menu item "Off" of menu 1 of menu item "Shuffle" of menu 1 of menu bar item "Controls" of menu bar 1
    end tell
end tell

但是,它仅在系统语言为英语时才有效。我不想使用索引,所以我尝试在"Off""Shuffle""Controls"上使用localized string of,但它似乎不起作用。

测试代码:

tell application "iTunes" to get localized string of "Shuffle"

是否有另一种方法可以获取本地化字符串或至少避免对菜单栏项索引进行硬编码?

您需要知道键的名称,因为iTunes不使用英文菜单的标题作为键名。

要知道键名(iTunes 捆绑包中的"Localizable.strings"文件是 plist 格式):

  • 打开文本牧马人应用程序,拖动"/Applications/iTunes.app/Content/Resources/English.lproj/Localizable.strings"文件并将其放在 TextWrangler 应用程序的图标上。

    在文档中搜索"字符串>随机播放<"以获取密钥名字。


set iTunesPath to "/Applications/iTunes.app" as POSIX file as alias
set contr to localized string "8d2vvyy7c7" in bundle iTunesPath
set shuf to localized string "atmqaez8y8" in bundle iTunesPath
set off to localized string "ve0frz64yk" in bundle iTunesPath
activate application "iTunes"
tell application "System Events"
    tell process "iTunes"
        click menu item off of menu 1 of menu item shuf of menu 1 of menu bar item contr of menu bar 1
    end tell
end tell

我在 Xcode 的开发工具中打开了辅助功能检查器,看看我可以通过UI elements找到枚举的对象

首先,我运行它是为了得到一个好主意,它返回我需要启动的名称,menu bar 1

tell application "System Events to tell process "iTunes"
    UI elements
end tell

在此之后,您可以获得要使用的元素列表,一旦您运行tell menu bar 1以显示其UI elements

{menu bar item "Apple" of menu bar 1 of application process "iTunes" of application "System Events", menu bar item "iTunes" of menu bar 1 of application process "iTunes" of application "System Events", menu bar item "File" of menu bar 1 of application process "iTunes" of application "System Events", menu bar item "Edit" of menu bar 1 of application process "iTunes" of application "System Events", menu bar item "View" of menu bar 1 of application process "iTunes" of application "System Events", menu bar item "Controls" of menu bar 1 of application process "iTunes" of application "System Events", menu bar item "Store" of menu bar 1 of application process "iTunes" of application "System Events", menu bar item "Window" of menu bar 1 of application process "iTunes" of application "System Events", menu bar item "Help" of menu bar 1 of application process "iTunes" of application "System Events"}

我想接下来你可以使用这里返回的内容来迭代并找到项目,将其存储在变量中并使其对名称的依赖性降低,因为索引不会根据语言而更改,并且可以在Accessibility Inspector中验证,这将显示对象引用。

我尝试了以下方法:

tell menu bar 1
    set a to UI Elements
    return item 6 of a
end tell

输出:

menu bar item "Controls" of menu bar 1 of application process "iTunes" of application "System Events"

好的,更进一步,您将需要找到特定的菜单项和值。哪些值也受到语言更改的影响。

tell application "System Events" to tell process "iTunes"
    tell menu bar 1
        set a to UI elements
        set b to item 6 of a
        set c to UI elements of b
        -- c = {menu "Controls" of menu bar item "Controls" of menu bar 1 of application process "iTunes" of application "System Events"}
        tell menu "Controls"
            set d to item 16 of UI elements
            tell menu "Shuffle" of d
                --click menu item 1 (On)
                --click menu item 2 (Off)
        end tell
        end tell
    end tell
end tell

最新更新