I am new to the world of Cocoa programming, and I want to add Applescript support to my application. The examples at Apple\'s website seem out of date.
How do I add
If you want to send AppleScript from your application and need a sandboxed app, you need to create a temporary entitlement
You need to add those two keys in your info.plist
NSAppleScriptEnabled
OSAScriptingDefinition
MyAppName.sdef
...of course you have to change "MyAppName" to your app's name
Create a .sdef file and add it to your project. The further course now greatly depends on the needs of your application, there are:
-
Go here to find a detailed description and many details on their implementation: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_script_cmds/SAppsScriptCmds.html
I found working with Class and KVC Elements very complicated, as I just wanted to execute a single command, nothing fancy. So in order to help others, here's an example of how to create a new simple command with one argument. In this example it'll "lookup" one string like this:
tell application "MyAppName"
lookup "some string"
end tell
The .sdef file for this command looks like this:
Create a subclass of NSScriptCommand and name it MyLookupCommand
The MyLookupCommand.h
#import
@interface MyLookupCommand : NSScriptCommand
@end
The MyLookupCommand.m
#import "MyLookupCommand.h"
@implementation MyLookupCommand
-(id)performDefaultImplementation {
// get the arguments
NSDictionary *args = [self evaluatedArguments];
NSString *stringToSearch = @"";
if(args.count) {
stringToSearch = [args valueForKey:@""]; // get the direct argument
} else {
// raise error
[self setScriptErrorNumber:-50];
[self setScriptErrorString:@"Parameter Error: A Parameter is expected for the verb 'lookup' (You have to specify _what_ you want to lookup!)."];
}
// Implement your code logic (in this example, I'm just posting an internal notification)
[[NSNotificationCenter defaultCenter] postNotificationName:@"AppShouldLookupStringNotification" object:stringToSearch];
return nil;
}
@end
That's basically it. The secret to this is to subclass NSScriptCommand and override performDefaultImplementation. I hope this helps someone to get it faster...