【问题标题】:Add scriptable property to Cocoa application derived from NSObject <NSApplicationDelegate>?将脚本属性添加到从 NSObject <NSApplicationDelegate> 派生的 Cocoa 应用程序?
【发布时间】:2016-04-12 23:45:50
【问题描述】:

谁能给我举个例子。我只想通过 AppleScript 设置一个属性值。我已经浏览了所有可编写脚本的示例,它们的设置方式不同。

<?xml version="1.0" encoding="UTF-8"?>
<dictionary title="">
<suite name="Circle View Scripting" code="bccS" description="Commands and classes for     Circle View Scripting">
    <class name="application" code="capp" description="" >
        <cocoa class="NSApplication"/>

        <property name="circletext" code="crtx" type="text" description="The text that gets spun into a circle">
            <cocoa key="circleText"/>
        </property>
        <property name="myint" code="crmy" type="integer" description="The text that gets spun into a circle">
            <cocoa key="myInt"/>
        </property>
    </class>
</suite>

头文件:

// header 
@interface MyDelegate : NSObject <NSApplicationDelegate> 
{
    WebScriptObject *scriptObject;
    WebView *webView;
    NSWindow *window;
    NSInteger myInt; 
}

// implementation
- (BOOL)application:(NSApplication*)sender delegateHandlesKey:(NSString*)key 
{ 
    return key isEqualToString:@"myInt"] || [key isEqualToString:@"circleText"];;
}

-(NSInteger)myInt
{
    NSInteger myInteger = 42;
    return myInteger;
}

-(void)setMyInt:(NSInteger*)newVal
{
    // do nothing right now
    NSLog(@"SETTER  CALLED");
}

// Applescript 尝试设置属性“myInt”

tell application "BrowserConfigClient"  
set myint to 7
properties
end tell

最终,delegateHandlesKey 方法被调用,我能够为属性返回一个值,但永远不会调用 setter。提前谢谢...

【问题讨论】:

  • 猜测,KVC 可能正在内省您的 -setMyInt: 方法,看到它需要一个 NSInteger* 参数并失败,因为它不能强制 NSNumber* 进入。因此,它可能会退回到直接实例变量访问。你的方法应该只接受一个 NSInteger,而不是指向一个的指针。您也可以考虑在您的课程中定期覆盖 +accessInstanceVariablesDirectly 以返回 NO 以排除此类事情。
  • 谢谢肯。这可能也是一个问题,但并没有解决我的问题。调用 getter 但不调用 setter。回到绘图板...
  • 如果您更改了代码,您可能应该编辑您的问题以反映这一点。是否在没有调用 setter 的情况下设置了 ivar?另外,我会指出你的吸气剂实际上并没有得到 ivar 的价值。它只是返回一个恰好是常量 42 的本地变量。
  • 尝试 setMyInt_(7) 或将 self() 的 myInt 设置为 7,看看是否有帮助。至少在 applescript 应用程序中,您必须以这两种方式之一调用覆盖设置器

标签: objective-c cocoa applescript cocoa-scripting


【解决方案1】:

你的方法语句有错误...

-(void)setMyInt:(NSInteger*)newVal

不应有“*”,因为 NSInteger 不是“指针”变量。我在您的问题中看到 Ken Thomases 已经告诉过您这一点,因此请务必修复它。

因此,如果这不是您的问题,请查看您的 sdef 文件。我可以看到你没有关闭字典标签。您需要将其作为该文件的最后一行。

</dictionary>

我的 sdef 文件中的第二行也有这个...

<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">

【讨论】:

    猜你喜欢
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多