< Previous PageNext Page > Hide TOC

Manipulating Files

This task explains how to perform Finder-like operations on files using the NSWorkspace class.

Contents:

Opening and Showing Files
Opening URLs
Performing File Operations


Opening and Showing Files

NSWorkspace provides several methods for opening files:

To show a file in the Finder, use the selectFile:inFileViewerRootedAtPath: method.

Opening URLs

To open a URL with the default handler for the resource type, use the openURL: method. The URL can be either local or remote. For example, a local files are opened as if double-clicked in the Finder, and a web addresses are opened in the default web browser.

Performing File Operations

The NSWorkspace method performFileOperation:source:destination:files:tag: performs various file system operations on files, such as moving and copying. The following Objective-C code fragment shows how to copy a file at fullPath from source to destination:

int tag;
BOOL succeeded;
NSString *source, *destination, *fullPath;    // Assume these exist
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
NSArray *files = [NSArray arrayWithObject:fullPath];
 
succeeded = [workspace performFileOperation:NSWorkspaceCopyOperation
                       source:source destination:destination
                       files:files tag:&tag];

In this code fragment, on return succeeded contains YES if the operation succeeded, NO otherwise. Also, the method sets tag to a negative integer if the operation fails, 0 if the operation is performed synchronously and succeeds, and a positive integer if the operation is performed asynchronously and succeeds.



< Previous PageNext Page > Hide TOC


© 2002, 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-03-06)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.