调用处理程序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_
你必须"将outPipe设置为当前应用程序的(NSPipe's pipe())"才能调用"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()
自:
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