On 1 Dec 2012, at 22:13, [email protected] wrote:
> I am trying to run the following one line echo bash script using
> NSUserUnixTask:
>
> echo "hello" >/dev/stderr
No need to go any further than here.
A script containing the above line executes in the bash shell as is, however
NSUserUnixTask requires us, not surprisingly, to be explicit.
Hence the following executes as expected:
#!/bin/bash
echo "hello" >/dev/stderr
Presumably com.apple.foundation.UserScriptService shouldn't crash when
encountering a script whose command processor it cannot identify.
The following will launch a user script task from NSApplicationScriptsDirectory
in a sandboxed app.
Thanks to Fritz for pointing out that the error detection was not up to scratch.
#import "MGSAppDelegate.h"
NSString * MGSTaskStartException = @"TaskException";
@interface MGSAppDelegate()
- (void)startScript:(NSString *)scriptname withError:(NSError **)error;
@property NSUserUnixTask *unixTask;
@end
@implementation MGSAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSError *error = nil;
// make certain to identify the script command processor correctly
// #!/bin/bash
[self startScript:@"TestScript.bash" withError:&error];
}
- (void)startScript:(NSString *)scriptname withError:(NSError **)error
{
NSURL *userScriptsFolderURL = [[NSFileManager defaultManager]
URLForDirectory:NSApplicationScriptsDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:NO
error:error];
if (!userScriptsFolderURL || *error) {
[NSException raise:MGSTaskStartException format:@"Bad user scripts
folder URL"];
}
NSURL *userScriptURL = [NSURL fileURLWithPathComponents:
@[[userScriptsFolderURL path], scriptname]];
if (!userScriptURL) {
[NSException raise:MGSTaskStartException format:@"Bad user script URL"];
}
self.unixTask = [[NSUserUnixTask alloc] initWithURL:userScriptURL
error:error];
if (!_unixTask || *error) {
[NSException raise:MGSTaskStartException format:@"Bad user task"];
}
[_unixTask executeWithArguments:nil completionHandler:^(NSError *err) {
if (err) {
NSLog(@"User Unix task execute failed: %@", err);
} else {
NSLog(@"User Unix task execute okay");
}
} ];
}
@end
Jonathan
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]