Question

I've coded a small utility for Avid Media Composer video editors which parses an edit decision list and generates two separate text files of use to the editors. I'm able to save the first file no problem. But I can't quite figure out how to modify my URL to automatically save the second file.

Say the first file is named: FirstFile.txt

I'd simply like to automatically save the second as: FirstFile_SubCap.txt

I keep running afoul of the NSURL & NSString rules somehow, with errors galore. If anybody has some guidance on how to do this, I'd be grateful.

Here's my code so far. I've removed my error-riddled code but commented where and what I'm trying to accomplish.

NSSavePanel *saveFile = [NSSavePanel savePanel];
long result = [saveFile runModal];
if(result == NSOKButton){
    NSURL *selectedFile = [saveFile URL];
    NSString *writeFile = [_infoWINDOW string];
    [writeFile writeToURL:selectedFile atomically:YES encoding:NSUTF8StringEncoding error:nil];

    // If the following condition is met, save the second file...

    if ([_subCap isEqual: @"yes"]) {

        // Here's where I need to save the second file as selectedFile+"_SubCap.txt"
    }

Thanks in advance.

Chris Conlee

Was it helpful?

Solution

You have to turn the URL into a path string, break it apart by separating the extension from the full path, and then reconstruct it with the _SubCap extension appended. Code:

NSString *subCapString = .... // string to write to _SubCap.txt file
NSString *path = selectedFile.path;
NSString *extension = path.pathExtension;
NSString *strippedPath = [path stringByDeletingPathExtension];
NSString *newPath = [NSString stringWithFormat:@"%@_SubCap.%@", strippedPath, extension];
NSURL *subcapURL = [NSURL fileURLWithPath:newPath];
[subCapString writeToURL:subcapURL atomically:YES encoding:NSUTF8StringEncoding];

OTHER TIPS

Here's how to generate a URL by adjusting the filename of another:

NSURL *selectedFile = [saveFile URL];

NSString *subCapString = [NSString stringWithFormat:
                          @"%@_SubCap.%@",
                          [selectedFile URLByDeletingPathExtension].absoluteString,
                          selectedFile.pathExtension];

NSURL *subCapURL = [NSURL URLWithString:subCapString];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top