Re: Running Shell Script from Cocoa Application
Re: Running Shell Script from Cocoa Application
- Subject: Re: Running Shell Script from Cocoa Application
- From: deepak gopal <email@hidden>
- Date: Mon, 8 Oct 2007 21:34:51 +0530 (IST)
Hi
This is how I Run my Shell Scripts. This should help.
1. Get AuthorizationReference
- (BOOL)getAuthorizationRef {
OSStatus status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
kAuthorizationFlagDefaults, &authRef_);
AuthorizationItem myItems = {kAuthorizationRightExecute, 0, NULL, 0};
AuthorizationRights myRights = {1, &myItems};
AuthorizationFlags flags = kAuthorizationFlagDefaults |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize |
kAuthorizationFlagExtendRights;
status = AuthorizationCopyRights(authRef_, &myRights,
kAuthorizationEmptyEnvironment, flags, NULL);
if (status != errAuthorizationSuccess) {
return NO;
}
return YES;
}
2. Run the script:
char *args[2];
NSString *launchTool = @"/bin/sh";
args[0] = (char *)[ShellScript fileSystemRepresentation];
args[1] = NULL;
NSLog (@"args[1] = %s", args[1]);
OSStatus status =
AuthorizationExecuteWithPrivileges(authRef_, [launchTool UTF8String], 0,
args, NULL);
if (status == errAuthorizationCanceled) {
NSLog (@"Canceled error");
} else if (status != errAuthorizationSuccess) {
NSLog (@"Failure error");
}
3. Release authorizationRef:
- (void)applicationWillTerminate:(NSNotification *)aNotification {
AuthorizationFree(authRef_, kAuthorizationFlagDestroyRights);
}
DG
----- Original Message ----
From: Jaime Magiera <email@hidden>
To: Cocoa Developers <email@hidden>
Sent: Sunday, 7 October, 2007 6:15:30 AM
Subject: Re: Running Shell Script from Cocoa Application
On Oct 7, 2007, at 5:09 AM, Daniel Dalquen wrote:
> That's what I want, too, and I am using AuthorizationServices. I'm
> just not sure, whether I am using it correctly... I need to copy a
> lauchd plist file to /Library/LaunchDeamons, unload an existing
> launchd config and load the new one. So, in my app I call an
> installer tool with AuthorizationExecuteWithPrivileges() and the
> installer then calls setuid(0) before doing the things described
> above. Is that correct or how should I do it better?
That's what I did for a tool that loads/unloads a kext (controlled
via dashboard widget), and it works well.
Jaime Magiera
Sensory Research Network
http://www.sensoryresearch.net
_______________________________________________
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
Flying to Bangalore or Bhopal? Search for tickets at http://in.farechase.yahoo.com
_______________________________________________
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