问题
I used this code in Objective C:
@implementation KDOrderInfo
- (id)performDefaultImplementation {
NSString *theRequest = [self directParameter];
NSDictionary *arguments = [self evaluatedArguments];
NSLog(@"arguments = %@ %ld",arguments, [arguments count]);
NSLog(@"theRequest----> %@",theRequest);
/*.......
.....*/
return @YES
}
This code works OK in Objective C. I converted this code to Swift code as follows:
class OrderInfo : NSScriptCommand {
override func performDefaultImplementation() -> AnyObject! {
let theRequest: AnyObject! = self.directParameter
let arguments = self.evaluatedArguments
println("arguments = \(arguments) argumentsCount = \(arguments.count)")
println("theRequest----> \(theRequest)")
/*.......
.....*/
return "OK"
}
}
when I run my applescript then I get the error (InfoMaker is the name of my app): InfoMaker got an error: The handler some object is not defined.
The method :
override func application(theApp:NSApplication,delegateHandlesKey theKey:String) -> Bool{
println("scripting key = \(theKey)");
let thekeys = ["pathToXML", "saveXMLFlag", "webserviceName","methodName","pathToInfoFiles","fileToCheck","scriptingProperties"]
if thekeys.containsString(theKey.lowercaseString, searchcase:NSStringCompareOptions.CaseInsensitiveSearch) {
//[self prefsToVars];
println("YES")
return true;
} else {
println("NO")
return false
}
}
in my app delegate responds OK to the applescript.
I tried also with the "SimpleScriptingVerbs" example from Apple and I get the same error in my Swift implementation. I hope somebody can help find the problem and have a suggestion.
回答1:
In the meantime, I was able to find a solution to the problem and get the app working with script commands.
The cocoa class name for your command you must mention in the script definition file (.sdef
) is not the same as the name of the class in XCode (Swift), as it is the case for Objective-C.
Instead you have to prepend the project name/module to the class like this: MyProject.MySwiftCommandClassName
If in doubt what the right name is, in your Swift class, you can use the following code to get the correct name to use in your .sdef
file:
class func classString() -> String {
return NSStringFromClass(self)
}
Your .sdef
file may look similar to this:
<dictionary title="MyAp">
<suite name="MyAp Suite" code="MyAp" description="MyAp Scripts">
<command name="myCommand" code="MyApMyCm" description="...">
<cocoa class="MyProject.MySwiftCommandClassName"/>
[...]
来源:https://stackoverflow.com/questions/25605805/swift-nsscriptcommand-performdefaultimplementation