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

时间:2015-01-25 02:40:15

标签: macos applescript 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

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

测试代码:

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

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

2 个答案:

答案 0 :(得分:3)

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

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

  • 打开TextWrangler应用程序,拖动 "的 /Applications/iTunes.app/Contents/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

答案 1 :(得分:1)

我在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