如何通过一个任意的AppleScript记录可可在编写脚本的应用程序? [英] How to pass an arbitrary AppleScript Record to Cocoa in a scriptable app?

查看:279
本文介绍了如何通过一个任意的AppleScript记录可可在编写脚本的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.sdef XML文件中描述的AppleScript字典Cocoa应用程序。所有的AppleScript类,命令等在sdef定义正在属性

I have a Cocoa application with an AppleScript dictionary described in a .sdef XML file. All of the AppleScript classes, commands, etc. defined in the sdef are working property.

除了我的提交表单命令。 提交表单命令是我试图传递一个参数是将信息从AppleScript的可可任意哈希表只有命令。我想这应该通过传递一个AppleScript 进行记录将被自动转换为的NSDictionary 的可可一边。

Except for my "submit form" command. The "submit form" command is my only command attempting to pass a parameter that is an arbitrary hashtable of information from AppleScript to Cocoa. I assume this should be done by passing an AppleScript record which will be automatically converted to an NSDictionary on the Cocoa side.

tell application "Fluidium"
    tell selected tab of browser window 1
        submit form with name "foo" with values {bar:"baz"}
    end tell
end tell

在使用值参数是记录 - 我有麻烦> 的NSDictionary 参数。注意,该记录/字典的键不能被公知/预先定义的。他们是任意的。

The "with values" parameter is the record -> NSDictionary parameter i am having trouble with. Note that the keys of the record/dictionary cannot be known/defined in advance. They are arbitrary.

下面是我sdef XML的这个命令的定义:

Here is the definition of this command in my sdef XML:

<command name="submit form" code="FuSSSbmt" description="...">
    <direct-parameter type="specifier" optional="yes" description="..."/>
    <parameter type="text" name="with name" code="Name" optional="yes" description="...">
        <cocoa key="name"/>
    </parameter>
    <parameter type="record" name="with values" code="Vals" optional="yes" description="...">
        <cocoa key="values"/>
    </parameter>
</command>

和我有一个标签对象,这是为了响应这个命令在sdef:

And I have a "tab" object which responds to this command in the sdef:

<class name="tab" code="fTab" description="A browser tab.">
    ...
    <responds-to command="submit form">
        <cocoa method="handleSubmitFormCommand:"/>
    </responds-to>

和可可:

- (id)handleSubmitFormCommand:(NSScriptCommand *)cmd {
    ...
}

在标签对象正确响应所有其他AppleScript命令我已经定义。在标签的对象也响应提交表单命令,如果我不发送可选的使用值参数。所以我知道我有正确的基本设置。唯一的问题似乎是任意记录 - > 的NSDictionary 参数

当我在执行上面的AppleScript 的AppleScript Editor.app <​​/ code>,我得到了可可身边这个错误:

When I execute the AppleScript above in AppleScript Editor.app, I get this error on the Cocoa side:

+[NSDictionary scriptingRecordWithDescriptor:]: unrecognized selector sent to class 0x7fff707c6048

而这其中的一面的AppleScript:

and this one on the AppleScript side:

error "Fluidium got an error: selected tab of browser window 1 doesn’t understand the submit form message." number -1708 from selected tab of browser window 1

谁能告诉我,我缺少的是什么?仅供参考整个应用程序是开源GitHub上:

Can anyone tell me what I'm missing? For reference the entire application is open source on GitHub:

http://github.com/itod/fluidium

推荐答案

右键 - NSDictionaries和AppleScript记录看起来他们会混淆,但它们实际上并不(NSDictionaries使用对象键 - 说字符串),其中AppleScript的使用记录四个字母字符codeS(感谢他们的AppleEvent /经典的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).

请参阅此线程苹果的AppleScript的实施者的邮件列表

那么,你真正需要做的,你的情况,是解压你有AppleScript的记录,并把它翻译成你的NSDictionary。你可以写在code全部由自己,但它的复杂和潜水深入到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.

然而,这项工作实际上已经为你做了一些垫层code为 appscript / appscript-objc (appscript是Python和Ruby和Objective-C的库,让你不必实际使用的AppleScript与AppleScriptable应用程序通信。appscript-objc可以使用,你会用可可脚本,但较少的苏茨基限制该技术。)

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.)

在code是在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.

有关其他的Google,还有另一种方式来做到这一点,这并不是使用appscript:的 ToxicAppleEvents 。有一个在那里,翻译词典进入苹果事件记录的方法。

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.

这篇关于如何通过一个任意的AppleScript记录可可在编写脚本的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆