Getting file name and path from nsfilewrapper / nstextattachment to NSAttributedString

I have a basic NSTextView with rich text and graphics (in IB). What I would like to get is the path and file name of any images being dragged so that I can pass them to another class.

I am new to NSAttributedString, but I have a loop in which it enumerateAttributesInRange:options:usingBlock:searches NSAttachmentAttributeName, and everything works fine. But delving deeper, I fall into the fileWrapper class and clearly fail to give me the path to the element .

How do I get the name and path of an NSTextAttachment?

Related: Is there an easier way to get them all and then go through the attributes?

Thank you so much!

+5
source share
1 answer

NSFileWrapper , , NSData NSFileWrapper regularFileContents. . , :

NSTextView NSDraggingDestination Protocol draggingEntered:, NSPasteboardItem, . inode NSMutableDictionary, NSFileWrapper inode , . , NSTextView NSAttributedString, inode .

- (NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender {

    // get pasteboard from dragging operation

    NSPasteboard *pasteboard = [sender draggingPasteboard];

    NSArray *pasteboardItems = [pasteboard pasteboardItems];

    for ( NSPasteboardItem *pasteboardItem in pasteboardItems ) {

        // look for a file url type from the pasteboard item

        NSString *draggedURLString = [pasteboardItem stringForType:@"public.file-url"];

        if (draggedURLString != nil) {

            NSURL *draggedURL = [NSURL URLWithString:draggedURLString];

            NSString *draggedPath = [draggedURL path];

            NSLog(@"pathname: %@", draggedPath);

            // do something with the path

            // get file attributes

            NSDictionary *draggedAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:draggedPath error:nil];

            if ( draggedAttributes == nil)
                continue;

            // the NSFileWrapper allows access to the absolute file via NSFileSystemFileNumber
            // put the path and the inode (returned as an NSNumber) into a NSMutableDictionary 

            NSNumber *draggedInode = [draggedAttributes objectForKey:NSFileSystemFileNumber];

            [draggedFiles setObject:draggedPath forKey:draggedInode];
        }

    }

    return [super draggingEntered:sender];
}

, , , , ( ), , , , . , , .

+8

All Articles