0

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!

Mike97
  • 596
  • 5
  • 20
  • As stated above I can run my script that executes the packages required, but there is a way to redirect the stdout and stdout to a scrollwiew using the "FILE *pipe = NULL;" part? – Mike97 Jan 11 '14 at 12:40
  • Just for info, my app work in 10.6 to 10.9.... I also implemented a Helper tool (SMJobbless + XPC), but I can not figure out how to launch the privileged task – Mike97 Jan 11 '14 at 12:45

0 Answers0