I would like to launch a packages (.pkg extensions) when I press a "Start" button on my application. The packages in question is in my Resource folder. The first make some preliminary tasks, another at the end of the process. They do not have the payload, but only executes the script inside them. I would have done so in bash:
installer -pkg $Resource_folder/package/info.pkg -target /
installer -pkg $Resource_folder/package/post.pkg -target /
...how can I do this in cocoa? Considering that "installer" command also provides "-target" options:
- (IBAction)runpackage:(id)sender
{
/*
NSTask *task = [[NSTask alloc] init];
task.launchPath = @"/usr/sbin/installer";
NSString* package_path = [[NSBundle mainBundle] pathForResource:@"pakage/info" ofType:@"pkg"];
NSLog(@"%@", package_path);
[task setArguments:[[[NSArray alloc] initWithObjects:
@"-pkg", package_path, @"-target", @"/", nil]
autorelease]];
[task launch];
[task waitUntilExit];
*/
OSStatus status;
AuthorizationRef authorizationRef;
status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
kAuthorizationFlagDefaults, &authorizationRef);
if (status != errAuthorizationSuccess)
NSLog(@"Error Creating Initial Authorization: %d", status);
AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
AuthorizationRights rights = {1, &right};
AuthorizationFlags flags = kAuthorizationFlagDefaults |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize |
kAuthorizationFlagExtendRights;
status = AuthorizationCopyRights(authorizationRef, &rights, NULL, flags, NULL);
if (status != errAuthorizationSuccess)
NSLog(@"STUFF Copy Rights Unsuccessful: %d", status);
NSString* path_STUFF = [[NSBundle mainBundle] pathForResource:@"runAllMypackage" ofType:@"sh"];
char *tool = "/bin/bash";
char *args[] = {NULL};
FILE *pipe = NULL;
status = AuthorizationExecuteWithPrivileges(authorizationRef, tool,
kAuthorizationFlagDefaults, args, &pipe);
if (status != errAuthorizationSuccess)
NSLog(@"Error: %d", status);
status = AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights);
}
I have edit this example to execute a script that run my pkg, and it works!