3

ハンドラーdoProcess_が呼び出されると、スクリプトは失敗し、ターゲット出力になります。

2012-12-09 22:59:24.193 cma[76284:303] *** -[cmaAppDelegate doProcess:]: unrecognized function fileHandleForReading. (error -10000)

任意のアイデアをいただければ幸いです。

script cmaAppDelegate
property parent : class "NSObject"
property inputUser : missing value
property inputPass : missing value
property outPipe : missing value
property outFileHandle : missing value
property theResult : missing value

on applicationWillFinishLaunching_(aNotification)
    -- Insert code here to initialize your application before any files are opened 
end applicationWillFinishLaunching_

on applicationShouldTerminate_(sender)
    -- Insert code here to do any housekeeping before your application quits 
    return current application's NSTerminateNow
end applicationShouldTerminate_

on doShellScriptInBackground_callback_(theCommand, selectorName)
    -- create pipe for standard output, and get reading file handle from it tell current application's NSPipe to set outPipe to pipe()
    set outFileHandle to outPipe's fileHandleForReading()
    -- make task and launch it
    tell current application's NSTask to set theTask to alloc()'s init()
    tell theTask
        setLaunchPath_("/bin/sh")
        setArguments_({"-c", theCommand})
        setStandardOutput_(outPipe)
        -- the following line is needed or logging ceases at this point setStandardInput_(current application's NSPipe's pipe())
    end tell
    -- add observer for notification
    tell current application's NSNotificationCenter to set nc to defaultCenter()
    tell nc to addObserver_selector_name_object_(me, selectorName, current application's NSFileHandleReadToEndOfFileCompletionNotification, outFileHandle) -- launch task
    tell theTask to |launch|()
    -- tell file handler do its stuff in the background
    tell outFileHandle to readToEndOfFileInBackgroundAndNotify()
end doShellScriptInBackground_callback_


on dataIsReady_(notif)
    -- remove the observer
    tell current application's NSNotificationCenter to set nc to defaultCenter()
    tell nc to removeObserver_name_object_(me, current application's NSFileHandleReadToEndOfFileCompletionNotification, missing value) -- get the data from notification's userInfo dictionary
    set theData to notif's userInfo()'s valueForKey_("NSFileHandleNotificationDataItem")
    -- make it into a string
    set theResult to current application's NSString's alloc()'s initWithData_encoding_(theData, current application's NSUTF8StringEncoding)
    -- do something wih the result
    log theResult
end dataIsReady_

on doProcess_(sender)
    doShellScriptInBackground_callback_("ls -ltr", "dataIsReady:")
end doProcess_
4

1 に答える 1

1

「outPipeのfileHandleForReading()」を呼び出すには、「outPipeを現在のアプリケーション(NSPipeのpipe())に設定」する必要があります。

変化する:

on doShellScriptInBackground_callback_(theCommand, selectorName)
    -- create pipe for standard output, and get reading file handle from it tell current application's NSPipe to set outPipe to pipe()
    set outFileHandle to outPipe's fileHandleForReading()

に:

on doShellScriptInBackground_callback_(theCommand, selectorName)
    -- create pipe for standard output, and get reading file handle from it
    tell current application's NSPipe to set outPipe to pipe()
    set outFileHandle to outPipe's fileHandleForReading()

それでおしまい。私はそれをテストしました、そしてそれは私のために働きました。上記のコメントにその行を修正したようです。

徹底するために; その行は多くの方法で書くことができます:

tell current application's NSPipe to set outPipe to pipe()

と同じです

set outPipe to current application's (NSPipe's pipe())

tell current application's NSPipe
    set outPipe to pipe()
end
于 2013-01-10T21:21:33.417 に答える