• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSUserUnixTask and com.apple.foundation.UserScriptService crash [SOLVED]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSUserUnixTask and com.apple.foundation.UserScriptService crash [SOLVED]


  • Subject: Re: NSUserUnixTask and com.apple.foundation.UserScriptService crash [SOLVED]
  • From: email@hidden
  • Date: Sun, 02 Dec 2012 10:18:42 +0000

On 1 Dec 2012, at 22:13, email@hidden 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@hidden)

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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: NSUserUnixTask and com.apple.foundation.UserScriptService crash [SOLVED]
      • From: Quincey Morris <email@hidden>
References: 
 >NSUserUnixTask and com.apple.foundation.UserScriptService crash (From: "email@hidden" <email@hidden>)

  • Prev by Date: Re: NSApplicationScriptsDirectory access for helper tool
  • Next by Date: Re: NSUserUnixTask and com.apple.foundation.UserScriptService crash [SOLVED]
  • Previous by thread: Re: NSUserUnixTask and com.apple.foundation.UserScriptService crash
  • Next by thread: Re: NSUserUnixTask and com.apple.foundation.UserScriptService crash [SOLVED]
  • Index(es):
    • Date
    • Thread