I’ve got a subclass of NSObject that has methods which are called from my main AppDelegate.applescript file. Basically, I want to be able to do the reverse, and call a handler in my AppDelegate.applescript from a method in the .m file of my NSObject subclass.
I found the following posting: http://macscripter.net/viewtopic.php?id=30478 but even just copying and pasting the example from the posting into a new project, results in a crash. Is there a relatively straight-forward way of accomplishing this? Thanks in advance!
It depends a bit on whether you want to call the handler as a class method or instance method. If it doesn’t involve properties, class methods are pretty simple. Let’s say your handler is:
You need to limit your arguments to NSString/NSArray/NSDictionary/NSNumber, and you’ll need to define the method in a protocol in your .m file, like this:
Thanks for your quick response! I think for this project, I only need a instance method. I got it working with your examples, though I am getting a warning about method definition not being found. That said, it runs perfectly even with the warning. I assume the warning is because the ‘definition’ for the handler is in AppDelegate.applescript.
Also, I should correct my earlier statement… I do not have a subclass, but rather a category for an existing class.
The warning I get is: Method definition for ‘doThis’ not found.
AppDelegate.applescript
script AppDelegate
property parent : class "NSObject"
property window : missing value
property shortcutView : missing value -- LINKED IN IB
property shortcutObject : missing value -- LINKED IN IB TO OBJECT
on applicationWillFinishLaunching:aNotification
shorcutView's setAssociatedUserDefaultsKey:"helloWorld"
shortcutObject's executeBlock:"helloWorld"
end applicationWillFinishLaunching:
on doThis_(sender)
tell me to activate
display dialog "It worked!"
end doThis:
on applicationShouldTerminate:sender
return current application's NSTerminateNow
end applicationShouldTerminate:
end script