Ruby 是否提供了响应 OS X 上的 Apple 事件的机制?
我正在使用 Ruby-Tk 开发 OS X 桌面应用程序,并且我想为该应用程序提供一个 Apple Events 界面。这意味着应用程序将定义一个它将响应的 AppleScript 命令字典(对应于发送到应用程序的 Apple 事件),并且用户/其他应用程序可以使用 AppleScript 命令编写 Ruby-Tk 应用程序的脚本。其他脚本语言支持此类功能 - Python 通过 http: //appscript.svn.sourceforge.net/viewvc/appscript/py-aemreceive/ 和 Tcl 通过 http://tclae.sourceforge.net/。我一直在 Ruby 中寻找类似的功能,但一无所获。
一种可能的机制是 rubyobjc 桥,它提供了 Ruby 和 Objective-C 之间的低级接口,但是这个 gem 似乎很少使用,并且非常缺乏示例和文档,所以我不确定这是否是一个富有成效的追求之路。
注意:MacRuby 可能可以工作,但它与 Tk 不兼容,因此排除了 MacRuby。另外,RubyOSA 和 rb-appscript 也不是我想要的——它们允许 Ruby 将 Apple 事件发送到其他应用程序,而不是接收它们。
I'm working on a desktop application for OS X using Ruby-Tk, and I would like to provide an Apple Events interface for the application. This means that the application would define a dictionary of AppleScript commands that it would respond to (corresponding to Apple Events being sent to the application), and users/other applications could script the Ruby-Tk application with AppleScript commands. Other scripting languages support such functionality--Python through the py-aemreceive library at http://appscript.svn.sourceforge.net/viewvc/appscript/py-aemreceive/ and Tcl through the tclAE library at http://tclae.sourceforge.net/. I've been looking for similar functionality in Ruby and have come up empty.
One possible mechanism is the rubyobjc bridge, which provides a low-level interface between Ruby and Objective-C, but this gem appears to be little-used and is sorely lacking in examples and documentation, so I am not sure if this would be a fruitful path to pursue.
NB: MacRuby might work but it is not compatible with Tk, so that rules out MacRuby. Also, RubyOSA and rb-appscript are not what I am looking for--they allow Ruby to send Apple Events to other applications, not receive them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在没有其他替代方案的情况下,看来我必须为我需要的 AppleEvent C API 部分编写自己的 Ruby 包装器:主要是 AEInstallEventHandler 和相关函数。幸运的是,苹果仍然支持这个 API,尽管它在苹果的开发者文档中已被降级为“遗留”状态(有趣的是,它并没有被弃用)。我要么通过 Ruby 的 ffi gem 集成这些函数,要么更有可能通过 Ruby 的 C API(我仍然需要深入研究);直接使用 C API 将减少对其他 gem/模块的依赖。如果进展顺利,我会将其作为宝石发布。
关于 Donal Fellows 的评论,我需要的是自定义 AppleEvents——他推荐的文档中通过 Tk 支持的事件很可能可以通过从 Ruby 的 Tk 接口调用 Tk 来访问。
In the absence of other alternatives, it appears I am going to have to write my own Ruby wrapper for the portions of the AppleEvent C API that I need: mainly AEInstallEventHandler and related functions. Fortunately Apple still supports this API even though it has been relegated to "legacy" status in Apple's developer docs (though, interestingly, it is not deprecated). I'll either integrate these functions via Ruby's ffi gem or, more likely, via Ruby's C API (which I still have to dig into); using the C API directly would reduce the need for dependencies on other gems/modules. If this goes well I will release it as a gem.
Regarding Donal Fellows' comment, my need is for custom AppleEvents--the ones supported via Tk in the docs he recommends can likely be accessed through calls to Tk from Ruby's Tk interface.
您可能需要检查 appscript 库(注意:似乎仅适用于 OS X 提供的 Ruby),或者尝试使用 MacRuby,它几乎包含了 OS X 中可用的所有 API,甚至是 C 的 API。
You might want to check the appscript library (note: Seems to only work with the OS X-provided Ruby), or try using MacRuby, which wraps pretty much everything of the APIs available in OS X, even the C ones.