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

发布于 2025-01-07 16:13:20 字数 1249 浏览 0 评论 0原文

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

遵循 Apple 的 字符串本地化指南,我在工作流程包中创建了语言环境资源文件夹(即/Contents/Resources/.lproj/ 文件夹)并将 Localized.strings 文件放置在包含 UTF-16 编码字符串映射的内部。我使用 AppleScript 构造的本地化字符串来调用它们。

当打包到 AppleScript 应用程序包中(经过测试和确认)时,效果很好,但是当在 Automator 生成的服务中使用完全相同的脚本和结构时,本地化会失败 – 我得到的只是原始令牌(请注意 本地化菜单名称 ServicesMenu.strings 拾取得很好 - 资源文件夹结构本身似乎不是问题)。

我的猜测是,问题在于,在 Automator 工作流程中,与应用程序包相反,本地化字符串 的上下文是 Automator(或 Automator Runner,因为它可能是),而不是真正的包,并且因此本地化查找失败。我尝试向服务添加一个捆绑包标识符 (CFBundleIdentifier),并通过 Automator 自己的 带有标识符 构造的捆绑包中的本地化字符串来引用该标识符,但这似乎仅限于使用 Automator 注册的操作包。

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

I have created an Automator workflow service (not Automator application!) under Mac OS X 10.7 (Lion) whose core is an AppleScript action. The action needs to notify the user at several points about its operations (including a possible abort) and I would like the messages to be localized in the user’s language.

Following Apple‘s string localization guidelines, I have created locale resource folders inside the workflow bundle (i.e. <bundle>/Contents/Resources/<lang>.lproj/ folders) and placed Localizable.strings files inside containing string mappings in UTF-16 encoding. I call these using the localized string of <string_mapping_token> construct of AppleScript.

This works just fine when packed into an AppleScript application bundle (tested and confirmed), but localization fails when the exact same script and structure are used inside an Automator generated service – all I get are the raw tokens (note the localized menu names in ServicesMenu.strings are picked up just fine – the resource folder structure itself does not seem to be the problem).

My guess is the issue is that inside an Automator workflow, as opposed to an application bundle, the context of localized string is Automator (or Automator Runner, as it may be), not the bundle proper, and thus localization lookup fails. I have tried adding a Bundle identifier (CFBundleIdentifier) to the service and referring to that via Automator’s own localized string in bundle with identifier <identifier> construct, but that seems to be restricted to action bundles registered with Automator.

Is there a workaround for this issue allowing me to use AppleScript’s native localization mechanism inside a self contained AppleScript service?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北渚 2025-01-14 16:13:20

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

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

编辑:以下是对我有用的示例:

我创建了一个新的服务工作流程,它接收任何应用程序中的文本,包含 Run 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 属性)。接下来,将 Localized.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 脚本包进行的额外测试,添加了相同的资源也运行正常,所以看起来只要您使用有效的包目录结构的路径,任何事情都可以。

The localized string command doesn't seem to have the right context in an Automator service, but you can use any bundle that has the resources in the usual places. If you know where the bundle is (your service workflow, a containing application, etc), you can specify that path in the command, e.g.

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

EDIT: the following is an example of what works for me:

I created a new service workflow that receives text in any application, consisting of a Run AppleScript action:

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

I named the workflow "localize test", and saved it in the default ~/Library/Services folder (this is the path is in the myPath property). Next, a Localizable.strings file (UTF-16 created with BBEdit) was placed in the service bundle in the /Contents/Resources/English.lproj folder that contains the following:

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

An additional test using an AppleScript script bundle on my Desktop with the same resources added also worked OK, so it looks like anything will do as long as you use a path to a valid bundle directory structure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文