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 设置为当前应用程序的(NSPipe 的 pipe())”才能调用“outPipe 的 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()

至:

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 回答