Call AppleScript handler from Objective C

Hi all,

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:

on doThis:x andThat:y

It would be:

[NSClassFromString(@"ASClass") doThis:x andThat:y];

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:

@protocol ASClass <NSObject>

+ (NSArray *)doThis:(NSString *)string andThat:(NSString *)other;

@end

Obviously you need to use the correct classes in there.

If you need to call it as an instance method, you usually need an IBOutlet, although if it’s your app deluge you can use:

[[NSApp delegate] doThis:x andThat:y];

Hi Shane,

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

.h file:


#import "MASShortcut.h"

@interface MASShortcut (BlockClass)

- (void)executeBlock:(NSString *)keyName;
- (void)doThis:self;

@end

.m file


@implementation MASShortcut (BlockClass)

- (void)executeBlock:(NSString *)keyName {
    
    [MASShortcut registerGlobalShortcutWithUserDefaultsKey:keyName handler:^{
        [[NSApp delegate] doThis:self];
    }];

}

@end


That’s why you need to add the protocol to your MASShortcut.m file.

I’m a bit confused. Are you saying I need to add a @protocol to the MASShortcut implementation file which defines the doThis handler?

Yes.

Haha. Ok. Somehow I read right over you explicitly saying that in your original post. If I ask you one more time will your answer change?