AppleScript opens a doc, handleOpenScriptCommand: raises exception

  • I'm adding AppleScriptability to my app.  My app can open its documents from
    its File menu.  But when I execute an AppleScript [1] to do open one,
    somebody sends -count to a NSCFString, and I get an exception in the Console
    output [2].

    This same AppleScript works fine if I change "tell application 'MyApp'" to
    instead "tell application 'TextEdit'".

    I set a breakpoint in [NSException raise] (*) and examined the stack trace
    [2.] when the exception occurs.  It occurs during -[NSApplication
    handleOpenScriptCommand:].  Duh.

    I don't know where to look.  Before the error occurs, my
    -applicationDidFinishLaunching completes successfully.
    My NSDocumentController's subclass'
    -openDocumentWithContentsOfURL:display:error:
    and NSDocument subclass' -loadDataRepresentation:ofType:
    never run; apparently the exception occurs within the Cocoa framework,
    before they are even invoked.

    The exception suggests that some argument in the NSScriptCommand* passed to
    handleOpenScriptCommand, which is expected to respond to count, is in fact a
    NSString.  But that doesn't make any sense.  The script debug output [3]
    shows only one argument, the file:// url of the document to be opened, a
    direct parameter.  That should be all it needs, and all it looks for.

    To see if this might be a Launch Services "environmental" problem, I tried
    this on another Mac, but got the same result.

    I'll try any reasonable suggestion...Thanks!

    Jerry Krinock

    (*) Thanks to "Advanced Mac OS X Programming", Dalrymple, Hilleglass for
    this cute trick.  In the debugger console, at the (gdb) prompt, type
      break -[NSException raise]

    **************************************
    1.  My AppleScript.
    **************************************

    tell application "Finder"
        set fileAlias to alias
          "JMiniHD:Users:jk:Migrations:m1.bdogmigr"
    end tell

    tell application "MyApp"
        activate
        open fileAlias
    end tell

    **************************************
    2.  Console output
    **************************************

    Command: Standard Suite.open
      Direct Parameter: file://localhost/Users/jk/Migrations/m1.bdogmigr
        Receivers: (null)
        Arguments: {
            "" = file://localhost/Users/jk/Migrations/m1.bdogmigr;
        }
    *** -[NSCFString count]: selector not recognized [self = 0x5afaf0]
    *** -[NSCFString count]: selector not recognized [self = 0x5afaf0]
    Result: (null)
    Error: 8 "NSInternalScriptError";

    **************************************
    3.  Stack Trace
    **************************************

    in -[NSException raise]
    in +[NSException raise:format:]
    in -[NSObject doesNotRecognizeSelector:]
    in -[NSObject(NSForwardInvocation) forward::]
    in _objc_msgForward
    in -[NSApplication _doOpenFiles:]
    in -[NSApplication(NSAppleEventHandling) _handleAEOpenDocuments:]
    in -[NSApplication(NSScripting) handleOpenScriptCommand:]
    in objc_msgSendv
    in -[NSInvocation invoke]
    in -[NSInvocation invokeWithTarget:]
    in -[NSScriptCommand executeCommand]
    in -[NSScriptingAppleEventHandler handleCommandEvent:withReplyEvent:]
    in -[NSAppleEventManager dispatchRawAppleEvent:withRawReply:handlerRefCon:]
    in _NSAppleEventManagerGenericHandler
    in aeDispatchAppleEvent
    in dispatchEventAndSendReply
    in aeProcessAppleEvent
    in AEProcessAppleEvent
    in _DPSNextEvent
    in -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:]
    in -[NSApplication run]
    in NSApplicationMain
    in main at main.m:7
  • Despit the fact that the log of the command looked normal, the problem is
    apparently in my sdef, because:

    If I turn off the Applecscript switch in info.plist, or replace my sdef with
    the one from the Sketch sample code, it is fixed.

    If I learn anything interesting, I'll reply again.