3

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.

Hendrik
  • 5,085
  • 24
  • 56
Kader Mokhefi
  • 341
  • 4
  • 15

2 Answers2

13

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"/>
            [...]
Hendrik
  • 5,085
  • 24
  • 56
Kader Mokhefi
  • 341
  • 4
  • 15
  • 1
    Thank you for providing this solution. Works for me. Much appreciated! – Hendrik Feb 13 '15 at 14:03
  • Agreed. This just saved me a lot of time. – Bryan Dec 05 '18 at 03:01
  • This answer got me to my personal breakthrough. My project name has a space in it, which needed to be an underscore _ - your function did it – sdailey Apr 29 '20 at 03:14
  • I'm not clear about your answer, how you got it to work. Did you use `performDefaultImplementation` or not? Because of the name changes between the question and the answer, I'm not sure that I follow what you did to the function, if anything. Could you please clarify? – August Jun 03 '23 at 04:54
1

If you prepend @objc(OrderInfo) to your class definition you won't need to add the project name.

@objc(OrderInfo) class OrderInfo : NSScriptCommand {
PeterZ
  • 11
  • 2