Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppleScript: Handler for Calling Shell Scripts Results in "Unrecognized function fileHandleForReading"

When handler doProcess_ is called, the script fails and results in target output:

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

Any ideas would be greatly appreciated.

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_
like image 744
user1890913 Avatar asked Aug 10 '26 00:08

user1890913


1 Answers

You have to "set outPipe to current application's (NSPipe's pipe())" in order to call "outPipe's fileHandleForReading()"

Change:

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()

To:

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()

That's it. I tested it, and it worked for me. It seems you just have amended that line to the comment above.

Just to be thorough; that line can be written many ways:

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

is the same as

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

and

tell current application's NSPipe
    set outPipe to pipe()
end
like image 140
olavimmanuel Avatar answered Aug 12 '26 13:08

olavimmanuel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!