代码之家  ›  专栏  ›  技术社区  ›  nrj

在cocoa应用程序中定义和执行简单的applescript命令

  •  6
  • nrj  · 技术社区  · 15 年前

    我正在尝试将一些脚本功能添加到我编写的cocoa应用程序中。我创造了一个 十二烷基硫酸钠 (脚本定义文件)用于我的项目。到目前为止,我已经成功地使用applescript访问了对象子(元素),但我一生都不知道如何调用方法(命令)。

    这是我的SDEF文件。

    <suite name="mySuite" code="mSUI" description="My Application Suite">
        <class name="application" code="capp" description="Top level scripting object.">
            <cocoa class="NSApplication"/>
            <!-- I can access these elements fine -->
            <element description="Test children." type="child" access="r">
                <cocoa key="myChildren"/>
            </element>
            <!-- Cannot seem to call this method :( -->
            <responds-to command="testmethod">
                <cocoa method="exposedMethod:"/>
            </responds-to>
        </class>
        <class name="child" code="cHIL" description="A Child." plural="children">
            <cocoa class="Child"/>
            <property name="name" code="pnam" description="The child name." type="text" access="r">
                <cocoa key="name"/>
            </property>
        </class>
        <command name="testmethod" code="tEST" description="Execute the test method" />
    </suite>
    

    以下是我的控制器类实现(这是我的应用程序的委托)

    霉菌控制者

    #import <Cocoa/Cocoa.h>
    
    @interface MyController : NSObject {
        NSMutableArray* myChildren;
    }
    // Some Methods
    
    @end
    

    mycontroller+脚本.m

    #import "MyController+Scripting.h"
    
    @implementation MyController (Scripting)
    
    // This works when I'm accessing the myChildren 
    - (BOOL)application:(NSApplication*)sender delegateHandlesKey:(NSString*)key { 
        NSLog(@"Key = %@", key);
      return ([key isEqualToString:@"myChildren"]);
    }
    // This does NOT work...
    - (void)exposedMethod:(NSScriptCommand*)command {
        NSLog(@"Invoked Test Script Method %@", [command description]);
    }
    
    @end
    

    最后,我正在尝试的applescript是:

    tell application "MyApplication"
        testmethod
    end tell
    

    它的反应是 "AppleScript Error - The variable testmethod is not defined."

    你知道我做错什么了吗?我觉得我错过了一些简单的东西,但我的谷歌搜索似乎没有找到任何有用的东西。

    3 回复  |  直到 13 年前
        1
  •  2
  •   Ben Stiglitz    15 年前

    事情看起来基本上是对的,但是 <command/> 应该是由两部分组成的代码(八个字符),而不是四个字符。

        2
  •  2
  •   Community CDub    8 年前

    我刚刚发布了一个关于如何添加 带参数的命令 在这里: https://stackoverflow.com/a/10773994/388412

    简而言之:我认为你应该 nsscript命令 超驰 -(无效)执行默认值 而不是在控制器中公开方法。至少那是我从 docs :

    “此命令是nsscriptcommand的一个子类,包含一个方法performDefaultImplementation。该方法重写nsscriptcommand中的版本“

    这对我来说很好,详情请看我的链接答案。

        3
  •  1
  •   Quinn Taylor    15 年前

    (实际上,我从未在Cocoa应用程序中添加过脚本功能,所以在黑暗中尝试一下。)

    我猜第一件事就是 exposedMethod: 接受一个参数,但我看不到在sdef或applescript中可以指定一个参数。事实上,看起来applescript正在处理 testmethod 作为变量,而不是命令。(也许它应该是 testmethod with ... “相反?”您可能需要定义 <parameter> <direct-parameter> sdef中命令的元素。

    另外,你不需要一个对象来调用这个方法吗?由于您的控制器是应用程序委托,我不确定其中的复杂性,但可能applescript尝试调用 testMethod: 而不是你的代表?

    我猜你已经看过苹果的示例代码和其他资源,但如果没有,这里有一些链接可能会有帮助:

    祝你好运!