Get path for NSFileWrapper
Asked Answered
S

3

11

Given an NSFileWrapper object (for a file or directory), is there any way to get the full path for the location of the actual file on the disk?

[fileWrapper filename] only returns the file name, not the path, so it isn't what I'm looking for.

Solemn answered 13/1, 2012 at 5:8 Comment(3)
In case you're using NSDocument.... zedkep.com/blog/index.php?/archives/…Platy
I am using NSDocument, and I did see that post. My only concern is that the document could be moved to a new location while open, and this is handled transparently. But if I try to access a file by the old path, it'll crash.Solemn
Is there a callback function in NSDocument to tell me when the file physically moves or is renamed?Solemn
S
5

No, there's no way to get the full path from NSFileWrapper.

Sportsmanship answered 13/1, 2012 at 5:16 Comment(1)
There is a way if you're using NSFileWrapper in NSDcoument. See my answer.Polariscope
P
3

If you're using NSDocument you can get the path of regular-file file wrappers with a little hack.

First create a NSFileWrapper subclass and overload the regular-file methods that receive a URL to store a copy of it.

@implementation RMFileWrapper

- (id) initWithURL:(NSURL *)url options:(NSFileWrapperReadingOptions)options error:(NSError *__autoreleasing *)outError {
    if (self = [super initWithURL:url options:options error:outError]) {
        self.originalURL = url;
    }
    return self;
}

- (BOOL) readFromURL:(NSURL *)url options:(NSFileWrapperReadingOptions)options error:(NSError *__autoreleasing *)outError {
    BOOL successful = [super readFromURL:url options:options error:outError];
    if (successful) {
        self.originalURL = url;
    }
    return successful;
}

@end

Then add this NSFileWrapper category:

@implementation NSFileWrapper(bestURLWithDocument)

- (NSURL*) bestURLInDocument:(SBDocument*)document {
    if (document.fileURL && self.filename) {
        NSString* path = [document.fileURL.path stringByAppendingPathComponent:self.filename];
        return [NSURL fileURLWithPath:path];
    } else if ([self isKindOfClass:[RMFileWrapper class]]) {
        RMFileWrapper *fileWrapper = (RMFileWrapper*) self;
        return fileWrapper.originalURL;        
    }
    return nil;
}

@end

bestURLInDocument: will return the url of the file-system node if available, or the original file url if not.

The above code assumes that you're not nesting directory file wrappers.

Polariscope answered 29/8, 2012 at 16:47 Comment(3)
How are you substituting the standard NSFileWrapper class for your custom subclass?Sharper
Also, this method guarantees little in terms of the returned URL. If autosaving is enabled, the location of your file is only ever good for reading OR writing, depending on when you manage to capture the NSURL instance. I've filed openradar.me/14692573Sharper
You can just subclass NSDocument/UIDocument then use self.fileURLCamboose
A
-1

You can append path components to the UIDocument fileURL.

Swift 5.2

document.fileURL.appendingPathComponent("MyFolder", isDirectory: true).appendingPathComponent("MyImage.png", isDirectory: false)
Anaerobe answered 22/3, 2020 at 17:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.