Search code examples
applescript-objc

How can I create a class method using AppleScriptObjC


I'm trying to override the +initialize method of a class using ASOC, but I cannot find a way to override a class method. Is it even possible?

Not to let any confusion possible about the language I'm talking about, here's some code:

script FLAppDelegate
    property parent: class "NSObject"

    -- -- Class Methods -- --
    -- Insert code here

end script

Solution

  • I've done some tests, and as far as I can tell, weird as it is, methods defined using AppleScriptObjC are both class and instance methods.

    Let's say I have an AppleScriptObjC file:

    script iTunesController
       property parent: class "NSObject"
    
       on playpause()
          tell application id "com.apple.iTunes" to playpause
       end playpause
    end script
    

    In an Objective-C method, both:

    - (void)callASOC
    {
       iTunesControllerInstance = [NSClassFromString(@"iTunesController") new];
       [iTunesControllerInstance playpause];
       [iTunesControllerInstance release];
    }
    

    and

    - (void)callASOC
    {
       [NSClassFromString(@"iTunesController") playpause];
    }
    

    will call the playpause handler in the AppleScriptObjC file. The latter formulation will generate a warning a compile time, but works.

    I was not able to find any documentation confirming or refuting this.