Search code examples
applescriptapplescript-objc

AppleScriptObjC: Can't create AVMIDIPlayer object


I'm having some trouble with a bit of AppleScript ObjC code. The problem line seems to be when I initialize the AVMIDIPlayer object (3rd line of repeat). It comes back with a -1 error, (AVAudioEngineManualRenderingStatusError). The next line then fails, because a Nil object doesn't have any methods.

Here's the relevant bits:

property NSURL : a reference to current application's NSURL
property AVMIDIPlayer : a reference to current application's AVMIDIPlayer
on open (filelist)
    repeat with each_item in filelist
        set myfile to quoted form of POSIX path of each_item as string
        set myMIDIFile to (NSURL's fileURLWithPath:myfile)
        set {myMIDIPlayer, theError} to (AVMIDIPlayer's alloc()'s initWithContentsOfURL:myMIDIFile soundBankURL:none |error|:(reference))
        myMIDIPlayer's prepareToPlay()
        myMIDIPlayer's play(myHandler)

Solution

  • The reason of the issue is quoted form of.

    Use it only in do shell script lines to escape string containing spaces and special characters, nowhere else.

    set myfile to POSIX path of each_item -- no coercion: POSIX path is string
    

    And handle theError 😉