且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在可编写脚本的应用程序中将任意 AppleScript 记录传递给 Cocoa?

更新时间:2023-11-24 19:03:52

没错——NSDictionaries 和 AppleScript 记录似乎会混合在一起,但实际上它们并没有(NSDictionaries 使用对象键——比如字符串)在 AppleScript 记录使用的地方四个字母的字符代码(感谢他们的 AppleEvent/Classic Mac OS 传统).

Right -- NSDictionaries and AppleScript records seem like they would mix, but they don't actually (NSDictionaries use object keys -- say strings) where AppleScript records use four letter character codes (thanks to their AppleEvent/Classic Mac OS heritage).

请参阅 Apple 的 AppleScript 实施者邮件列表上的此主题一个>

因此,在您的情况下,您实际需要做的是解压缩您拥有的 AppleScript 记录并将其翻译成您的 NSDictionary.您可以自己编写所有代码,但它很复杂,并且深入到 AE 管理器中.

So, what you actually need to do, in your case, is to unpack the AppleScript record you have and translate it into your NSDictionary. You could write the code all by yourself, but it's complicated and dives deep into the AE manager.

然而,这项工作实际上已经在 appscript/appscript-objc 的一些底层代码中为您完成(appscript 是一个适用于 Python 和 Ruby 以及 Objective-C 的库,它允许您与 AppleScriptable 应用程序进行通信,而无需实际使用 AppleScript.appscript-objc 可用于您将使用 Cocoa Scripting 的地方,但该技术的限制较少.)

However, this work has actually been done for you in some underlaying code for appscript/appscript-objc (appscript is an library for Python and Ruby and Objective-C that lets you communicate with AppleScriptable applications without actually having to use AppleScript. appscript-objc could be used where you would use Cocoa Scripting, but has less of the sucky limitations of that technology.)

代码在 sourceforge 上提供.几周前我向作者提交了一个补丁,这样你就可以为 appscript-objc 构建底层基础,这就是你在这种情况下所需要的:你需要做的就是打包和解包 Applescript/AppleEvent 记录.

The code is available on sourceforge. I submitted a patch a few weeks ago to the author so you could build JUST the underlaying foundation for appscript-objc, which is all you need in this case: all you need to do is pack and unpack Applescript/AppleEvent records.

对于其他 googlers,还有另一种方法可以做到这一点,即不使用 appscript:ToxicAppleEvents.那里有一种方法可以将字典翻译成 Apple 事件记录.

For other googlers, there's another way to do this, that's not using appscript: ToxicAppleEvents. There's a method in there that translates dictionaries into Apple Event Records.