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

编程入门 行业动态 更新时间:2024-10-19 06:26:59
本文介绍了如何在可编写脚本的应用程序中将任意 AppleScript 记录传递给 Cocoa?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个 Cocoa 应用程序,其中包含在 .sdef XML 文件中描述的 AppleScript 字典.sdef 中定义的所有 AppleScript 类、命令等都是工作属性.

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 到 Cocoa 的任意信息散列表传递.我认为这应该通过传递 AppleScript record 来完成,它将在 Cocoa 端自动转换为 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

带值"参数是我遇到问题的 record -> 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 {
    ...
}

tab"对象正确响应我定义的所有其他 AppleScript 命令.如果我不发送可选的带值"参数,选项卡"对象也会响应提交表单"命令.所以我知道我的基础设置正确.唯一的问题似乎是任意的 record->NSDictionary 参数.

The "tab" object correctly responds to all the other AppleScript commands I have defined. The "tab" object also responds to the "submit form" command if I don't send the optional "with values" param. So I know I have the basics setup correctly. The only problem seems to be the arbitrary record->NSDictionary param.

当我在 AppleScript Editor.app 中执行上面的 AppleScript 时,我在 Cocoa 端收到这个错误:

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/itod/fluidium

推荐答案

没错——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.

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

更多推荐

[db:关键词]

本文发布于:2023-04-17 14:55:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/915860.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   应用程序   脚本   如何在   Cocoa

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!