@peavine…
I found this code snippet I wrote as an exercise 3 or 4 years ago, buried in my library of code snippets, that I kept as they might be useful in future projects.
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
property myApp : a reference to current application
set shellTask to myApp's NSTask's new()
shellTask's setLaunchPath:"/bin/bash/"
shellTask's setArguments:{"-c", "whereis osascript"}
set shellOutputPipe to myApp's NSPipe's pipe()
set shellErrorPipe to myApp's NSPipe's pipe()
shellTask's setStandardOutput:shellOutputPipe
shellTask's setStandardError:shellErrorPipe
shellTask's |launch|()
shellTask's waitUntilExit()
if not (shellTask's isRunning() as boolean) then
set shellTaskStatus to shellTask's terminationStatus() as integer
if shellTaskStatus = 0 then -- Task Succeeded
set shellTaskOutputHandle to shellOutputPipe's fileHandleForReading()
set shellTaskOutputData to shellTaskOutputHandle's readDataToEndOfFile()
set shellTaskOutputString to myApp's NSString's alloc()'s initWithData:shellTaskOutputData encoding:(myApp's NSUTF8StringEncoding)
return shellTaskOutputString as text
else if shellTaskStatus = 1 then -- Task Failed
set shellTaskErrorHandle to shellErrorPipe's fileHandleForReading()
set shellTaskErrorData to shellTaskErrorHandle's readDataToEndOfFile()
set shellTaskErrorString to myApp's NSString's alloc()'s initWithData:shellTaskErrorData encoding:(myApp's NSUTF8StringEncoding)
set shellTaskErrorReason to shellTask's terminationReason() as integer
if shellTaskErrorReason = 1 then -- Task Failed Because of an Empty String Result, Or Has Error String Result
return shellTaskErrorString as text
else if shellTaskErrorReason = 2 then -- Task Properly Failed to Complete Execution for Unknown Reason, Maybe No Error String Result
return "Task Properly Failed to Complete Execution for Unknown Reason" as text
end if
end if
end if
I’m not suggesting it is a definitive way of using the NSTask class, as Shane would have a lot more experience with this stuff, but it certainly worked ok back on Sierra.
I know a lot of the NSTask function’s where marked for deprecation some time ago, so it may not work on the later MacOS’s.
The “if not (shellTask’s isRunning() as boolean) then” line can be eliminated, as the code execution waits at “shellTask’s waitUntilExit()” for the task to complete anyway, but I included it just for completeness, and to show all of the possibilities.
The “shellTask’s terminationReason()” and if clauses can also be excluded most of the time, but they Highlight the difference between an Error message failure, and a massive failure by the NSTask object itself.
-- If you change the line.
shellTask's setArguments:{"-c", "whereis osascript"}
--TO
shellTask's setArguments:{"-c", "whereis supermodels"}
You will see what I mean, as the error is merely an empty string, which is the same result you would get in the Terminal. because supermodels are not installed on the system unfortunately.
But you may well want to check for the empty string, even though it was sent to the StandardError pipe.
So it is an error, but not a failure error.
Anyway it might be useful stuff for you, or maybe not.
But Good Luck with it.
Regards Mark