hockeyman
hockeyman

Reputation: 1183

NSTask launch in xCode issue

I'm trying to use this code to get my ImageMagick library in app bundle, but it's very complicated:

-(id)init
{
    if ([super init])
    {
        NSString* bundlePath = [[NSBundle mainBundle] bundlePath];
        NSString* imageMagickPath = [bundlePath stringByAppendingPathComponent:@"/Contents/Resources/ImageMagick"];
        NSString* imageMagickLibraryPath = [imageMagickPath stringByAppendingPathComponent:@"/lib"];

        MAGICK_HOME = imageMagickPath;
        DYLD_LIBRARY_PATH = imageMagickLibraryPath;
    }
    return self;
}

-(void)composite
{
    NSTask *task = [[NSTask alloc] init];

    // the ImageMagick library needs these two environment variables.
    NSMutableDictionary* environment = [[NSMutableDictionary alloc] init];
    [environment setValue:MAGICK_HOME forKey:@"MAGICK_HOME"];
    [environment setValue:DYLD_LIBRARY_PATH forKey:@"DYLD_LIBRARY_PATH"];

    // helper function from
    // http://www.karelia.com/cocoa_legacy/Foundation_Categories/NSFileManager__Get_.m
    NSString* pwd = [Helpers pathFromUserLibraryPath:@"MyApp"];

    // executable binary path
    NSString* exe = [MAGICK_HOME stringByAppendingPathComponent:@"/bin/composite"];

    [task setEnvironment:environment];
    [task setCurrentDirectoryPath:pwd]; // pwd
    [task setLaunchPath:exe]; // the path to composite binary
    // these are just example arguments
    [task setArguments:[NSArray arrayWithObjects: @"-gravity", @"center", @"stupid hat.png", @"IDR663.gif", @"bla.png", nil]];
    [task launch];
    [task waitUntilExit];
}

Declaration of DYLD_LIBRARY_PATH and MAGICK_HOME identifiers (Solved)

UPDATE:

But when I try to build and run it, my app crashes. Crashes at:[task launch];.
Console message:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'working directory doesn't exist.'

How can I solve current problem?

UPDATE 2:

Current code:

- (id)initWithCoder:(NSCoder *)coder
{
NSString* bundlePath = [[NSBundle mainBundle] bundlePath];
NSString* imageMagickPath = [bundlePath stringByAppendingPathComponent:@"/Contents/Resources/ImageMagick"];
NSString* imageMagickLibraryPath = [imageMagickPath stringByAppendingPathComponent:@"/lib"];

MAGICK_HOME = imageMagickPath;
DYLD_LIBRARY_PATH = imageMagickLibraryPath;
[self composite];
}

-(void)composite
{
    NSTask *task = [[NSTask alloc] init];
    
    NSMutableDictionary* environment = [[NSMutableDictionary alloc] init];
    [environment setValue:MAGICK_HOME forKey:@"MAGICK_HOME"];
    [environment setValue:DYLD_LIBRARY_PATH forKey:@"DYLD_LIBRARY_PATH"];
    
    NSString* loc = [[NSString stringWithFormat:@"%@", MAGICK_HOME] retain];
    NSString* exe = MAGICK_HOME;
    
    [task setEnvironment:environment];
    NSString* pwd = @"/opt/local/lib/";
    [task setCurrentDirectoryPath:pwd];
    [task setLaunchPath:loc];
    NSLog(@"%@", loc);
    NSLog(@"%@", pwd);
    [task setArguments:[NSArray arrayWithObjects: @"-gravity", @"center", @"stupid hat.png", @"IDR663.gif", @"bla.png", nil]];
    [task launch];
    [task waitUntilExit];
}

And current error (in console):

*** NSTask: Task create for path '/Users/development/Library/Developer/Xcode/DerivedData/OGL-cahltqazoqxhrthkxztsqyvvodge/Build/Products/Debug/OGL.app/Contents/Resources/ImageMagick' failed: 22, "Invalid argument".  Terminating temporary process.

Upvotes: 2

Views: 1576

Answers (1)

Martin R
Martin R

Reputation: 539705

[task setLaunchPath:...] must be called with the path to the executable binary. In your "UPDATE 2" code you call it with a directory path.

Upvotes: 2

Related Questions