iTunes 中的本地化菜单栏项目字符串
Localized menu bar item strings in 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 bundle中的“Localizable.strings”文件是plist格式):
打开 TextWrangler 应用程序,将
“/Applications/iTunes.app/Contents/Resources/English.lproj/Localizable.strings”
文件并将其放在 TextWrangler 应用程序的图标上。
在文档中搜索"string>Shuffle<"获取密钥
名字.
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
找到哪些枚举对象
首先我运行这个想好了,哪个returns我需要开始的名字,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
以下代码将禁用 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 bundle中的“Localizable.strings”文件是plist格式):
打开 TextWrangler 应用程序,将 “/Applications/iTunes.app/Contents/Resources/English.lproj/Localizable.strings” 文件并将其放在 TextWrangler 应用程序的图标上。
在文档中搜索"string>Shuffle<"获取密钥 名字.
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
首先我运行这个想好了,哪个returns我需要开始的名字,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