如何在Automator工作流程中使用AppleScript的“本地化字符串”

时间:2012-02-19 00:50:37

标签: localization applescript automator

我在Mac OS X 10.7(Lion)下创建了一个Automator 工作流服务(不是Automator应用程序!),其核心是AppleScript操作。该操作需要在几个点上通知用户其操作(包括可能的中止),并且我希望消息以用户的语言进行本地化。

在Apple的string localization guidelines之后,我在工作流包中创建了语言环境资源文件夹(即<bundle>/Contents/Resources/<lang>.lproj/个文件夹),并将Localizable.strings个文件放在包含UTF-16编码的字符串映射中。我使用AppleScript的localized string of <string_mapping_token>结构调用它们。

当打包到AppleScript应用程序包(经过测试和确认)时,这很好用,但是当在Automator生成的服务中使用完全相同的脚本和结构时,本地化失败 - 我得到的只是原始令牌(请注意{{ 3 {}在ServicesMenu.strings中被选中就好了 - 资源文件夹结构本身似乎不是问题。)

我的猜测是问题是在Automator工作流程中,与应用程序包相反,localized string的上下文是Automator(或Automator Runner,因为它可能是),而不是捆绑适当的,因此本地化查找失败。我尝试将一个Bundle标识符(CFBundleIdentifier)添加到服务中并通过Automator自己的localized string in bundle with identifier <identifier>构造引用它,但这似乎仅限于在Automator中注册的动作包。

是否有解决此问题的方法,允许我在自包含的AppleScript服务中使用AppleScript的本机本地化机制?

1 个答案:

答案 0 :(得分:3)

本地化字符串命令似乎在Automator服务中没有正确的上下文,但您可以使用任何具有常用位置资源的包。如果您知道捆绑包的位置(您的服务工作流程,包含应用程序等),您可以在命令中指定该路径,例如

get localized string of "testing" in bundle file "path:to:your:bundle"

编辑:以下是对我有用的一个例子:

我创建了一个新的服务工作流程,可以在任何应用程序中接收文本,包括运行AppleScript 操作:

property myPath : (path to library folder from user domain as text) & "Services:localize test.workflow"

on run {input, parameters}

    try
        display alert getLocalizedString("TESTING") & return & getLocalizedString("NO_ERROR") message "Input Text:    " & quoted form of (input as text)
    on error errmess number errnum
        display alert "Error" & errnum message errmess
    end try

    return input
end run

on getLocalizedString(theString)
    get localized string of theString in bundle file myPath
end getLocalizedString

我将工作流命名为“localize test”,并将其保存在默认的〜/ Library / Services 文件夹中(此路径位于 myPath 属性中)。接下来,将 Localizable.strings 文件(使用BBEdit创建的UTF-16)放在 /Contents/Resources/English.lproj 文件夹中的服务包中,该文件夹包含以下内容: :

/* Automator localization test */
"NO_ERROR" = "There seems to have been no error.";
"TESTING" = "Hmmm, works OK for me...";

使用添加了相同资源的桌面上使用AppleScript脚本捆绑包的附加测试也可以正常工作,所以只要您使用有效捆绑目录结构的路径,它就会发生任何事情。