Xojo Developer Conference
25/27th April 2018 in Denver.
MBS Xojo Conference
6/7th September 2018 in Munich, Germany.

Platforms to show: All Mac Windows Linux Cross-Platform

NSFileWrapperMBS class

Type Topic Plugin Version macOS Windows Linux Console & Web iOS
class Cocoa MBS CocoaBase Plugin 15.0 Yes No No Yes, macOS only No
Function: The NSFileWrapper class provides access to the attributes and contents of file-system nodes.
// insert a file to textview

Public Sub InsertFile(textview as NSTextViewMBS, f as FolderItem)
// read to file
dim b as BinaryStream = BinaryStream.Open(f)
dim s as string = b.Read(b.Length)

// build wrapper
dim fileWrapper as NSFileWrapperMBS = NSFileWrapperMBS.initRegularFileWithContents(s)
fileWrapper.preferredFilename = f.name

// make attachment
dim fileAttachment as new NSTextAttachmentMBS(fileWrapper)
dim attributedString as NSAttributedStringMBS = NSAttributedStringMBS.attributedStringWithAttachment(fileAttachment)

// add to a NSTextViewMBS
textview.insertText attributedString

End Sub
A file-system node is a file, directory, or symbolic link. Instances of this class are known as file wrappers.

File wrappers represent a file-system node as an object that can be displayed as an image (and possibly edited in place), saved to the file system, or transmitted to another application.

There are three types of file wrappers:
  • Regular-file file wrapper: Represents a regular file.
  • Directory file wrapper: Represents a directory.
  • Symbolic-link file wrapper: Represents a symbolic link.

A file wrapper has these attributes:
  • Filename. Name of the file-system node the file wrapper represents.
  • file-system attributes. See NSFileManager Class Reference for information on the contents of the attributes dictionary.
  • Regular-file contents. Applicable only to regular-file file wrappers.
  • File wrappers. Applicable only to directory file wrappers.
  • Destination node. Applicable only to symbolic-link file wrappers.

Feedback, Comments & Corrections

Reading Options

Constant Value Description
NSFileWrapperReadingImmediate 1 If reading with this option succeeds, then subsequent uses of fileWrappers, regularFileContents, symbolicLinkDestinationURL, and serializedRepresentation sent to the file wrapper and all its child file wrappers will fail and return nil only if an actual error occurs (for example, the volume has disappeared or the file server is unreachable)—not as a result of the user moving or deleting files.

For performance reasons, NSFileWrapper may not read the contents of some file packages immediately even when this option is chosen. For example, because the contents of bundles (not all file packages are bundles) are immutable to the user, NSFileWrapper may read the children of such a directory lazily.

You can use this option to take a snapshot of a file or folder for writing later. For example, an application like TextEdit can use this option when creating new file wrappers to represent attachments that the user creates by copying and pasting or dragging and dropping from the Finder to a TextEdit document. Don't use this option when reading a document file package, because that would cause unnecessarily bad performance. For example, an application wouldn't use this option when creating file wrappers to represent attachments as it's opening a document stored in a file package.

Available in OS X v10.6 and later.
NSFileWrapperReadingWithoutMapping 2 Whether file mapping for regular file wrappers is disallowed.

You can use this option to keep NSFileWrapper from memory-mapping files. This is useful if you want to make sure your application doesn't hold files open (mapped files are open files), therefore preventing the user from ejecting DVDs, unmounting disk partitions, or unmounting disk images. In OS X v10.6 and later, NSFileWrapper memory-maps files that are on internal drives only. It never memory-maps files on external drives or network volumes, regardless of whether this option is used.

Available in OS X v10.6 and later.

Writing Options

Constant Value Description
NSFileWrapperWritingAtomic 1 Whether writing is done atomically.

You can use this option to ensure that, when overwriting a file package, the overwriting either completely succeeds or completely fails, with no possibility of leaving the file package in an inconsistent state. Because this option causes additional I/O, you shouldn't use it unnecessarily. For example, don't use this option in an override of -[NSDocument writeToURL], because NSDocument safe-saving is already done atomically.

Available in OS X v10.6 and later.
NSFileWrapperWritingWithNameUpdating 2 Whether descendant file wrappers’filename properties are set if the writing succeeds.

This option is necessary when your application passes a URL in the originalContentsURL parameter to the writeToURL method. Without using this option (and reusing child file wrappers properly), subsequent invocations of writeToURL would not be able to reliably create hard links in a new file package, because the record of names in the old file package would be out of date.

Available in OS X v10.6 and later.

This class has no sub classes.

Some methods using this class:

Some properties using for this class:

Some examples which use this class:

The items on this page are in the following plugins: MBS CocoaBase Plugin.

NSFileVersionMBS   -   NSFontDescriptorMBS

MBS Xojo PDF Plugins