Applescript 如何在脚本中继承Cocoa类

Applescript 如何在脚本中继承Cocoa类,applescript,Applescript,我是苹果公司的新手,但在iOS的Objective-C方面有多年的经验。我喜欢在AppleScript捆绑包中使用Cocoa类的能力。但我不明白我如何才能真正地将Cocoa类划分为子类。例如,我在AppleScript包“testLib”中编写: 但当我尝试在AppleScript中使用它时,我得到了一个错误: set carObj to Car of script "testLib" carObj's workWithCar() -- script Car doesn't understand

我是苹果公司的新手,但在iOS的Objective-C方面有多年的经验。我喜欢在AppleScript捆绑包中使用Cocoa类的能力。但我不明白我如何才能真正地将Cocoa类划分为子类。例如,我在AppleScript包“testLib”中编写:

但当我尝试在AppleScript中使用它时,我得到了一个错误:

set carObj to Car of script "testLib"
carObj's workWithCar() -- script Car doesn't understand retainCount message (sorry I have localized text and I don't understand how it looks like in English)
这意味着我的车不是真正的NSObject子类。但如果我在Xcode中创建Cocoa Apple脚本应用程序

script AppDelegate
property parent : class "NSObject"

on applicationWillFinishLaunching_(aNotification)
    log me's retainCount() as integer -- returns 1
end applicationWillFinishLaunching_

end AppDelegate

我去看看。在那里,我可以访问所有NSObject的方法,我认为苹果在从xib解构对象时做了一些神奇的转换。在我的AppleScript捆绑包中的代码中可以获得相同的效果吗?

这看起来像是在尝试创建一个没有术语的简单脚本库

(如果我错了,请纠正我)

下面是一个简单脚本库的示例:

on transform(sourceText, caseIndicator)
    set validCaseType to {"upper case", "lower case", "word case"}
    if caseIndicator is in validCaseType then
        -- create a Cocoa string from the passed text
        set the sourceString to current application's NSString's stringWithString:sourceText
        -- apply the indicated transformation to the Cocoa string
        if the caseIndicator is "upper case" then
            set the adjustedString to sourceString's uppercaseString()
        else if the caseIndicator is "lower case" then
            set the adjustedString to sourceString's lowercaseString()
        else if the caseIndicator is "word case" then
            set the adjustedString to sourceString's capitalizedString()
        end if
        -- convert from Cocoa string to AppleScript text
        return (adjustedString as text)

    else
        set errStr to caseIndicator & " is not valid case indicator" as text
        error errStr

    end if

end transform
将此保存为脚本包。和在其中一个脚本库文件夹中

我用了一个用户<代码>~/Library/Script Libraries/。如果它不存在,就创建它

你的应用可以有自己的脚本库

然后打开脚本的捆绑内容抽屉并检查:

Applescript/Objective-C库复选框

再次保存文件以设置更改

现在,在普通的Applescript中,您可以运行

tell script "test"
    set the adjustedText to transform("the cat", "upper case")

end tell
返回->“猫”


了解这一点意味着要使用代码

脚本库捆绑包将命名为“test”,代码为:

--  Not needed here --> property parent : class "NSObject"


on workWithCar()
    log me's retainCount() as integer

end workWithCar
正常的Applescript是:

tell script "test"
    set the theCar to workWithCar()
 end tell

返回->
*1*

是的,我想创建一个脚本库。您的回答告诉我脚本库表示为NSObject子类。我以前不知道,谢谢。但是其他课程呢?我可以将NSWindowController或任何其他Cocoa类子类化并重写其方法吗?老实说。我不确定。你想这样做的目的是什么?我只想在我的脚本库中使用UI。我想创建一个窗口并在其上放置一个webView,但只能在主线程上创建webView。我决定我可以在NSWindowController的windowDidLoad中完成。我可以创建NSWindow并显示它,但如果不将NSWindowController子类化,我无法完全控制该窗口。
tell script "test"
    set the theCar to workWithCar()
 end tell