gpt4 book ai didi

objective-c - AppleScript:调用 Shell 脚本的处理程序结果为 "Unrecognized function fileHandleForReading"

转载 作者:搜寻专家 更新时间:2023-10-30 20:25:40 28 4
gpt4 key购买 nike

调用处理程序 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 的 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

关于objective-c - AppleScript:调用 Shell 脚本的处理程序结果为 "Unrecognized function fileHandleForReading",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13796512/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com