Class

NSWorkspace

An NSWorkspace object can launch other apps and perform a variety of file-handling services.

Overview

There is one shared NSWorkspace object per app. You use the class method shared() to access it. For example, the following statement uses an NSWorkspace object to request that a file be opened in the TextEdit app:

[[NSWorkspace sharedWorkspace] openFile:@"/Myfiles/README"
    withApplication:@"TextEdit"];

You can use the workspace object to:

  • Open, manipulate, and get information about files and devices

  • Track changes to the file system, devices, and the user database

  • Get and set Finder information for files

  • Launch apps

Legacy Constants

Table 1 describes keys for an NSDictionary object containing information about an app. This dictionary is returned by active​Application() and launched​Applications, and is also provided in the user​Info of NSWorkspace notifications for app launch and termination.

Note that these constants are considered legacy.

Table 1

userInfo dictionary keys for activeApplication and launchedApplications and notifications for app launch and termination

Key

Value

@"NSApplication​Path"

The full path to the app, as a NSString object.

@"NSApplication​Name"

The app's name, as an NSString object.

@"NSApplication​Bundle​Identifier"

The app’s bundle identifier, as an NSString object.

@"NSApplication​Process​Identifier"

The app's process id, as an NSNumber object.

@"NSApplication​Process​Serial​Number​High"

The high long of the process serial number (PSN), as an NSNumber object.

@"NSApplication​Process​Serial​Number​Low"

The low long of the process serial number (PSN), as an NSNumber object.

Symbols

Accessing the Shared NSWorkspace Instance

class func shared()

Returns the shared NSWorkspace instance.

Accessing the NSWorkspace Notification Center

var notification​Center:​ Notification​Center

Returns the notification center for workspace notifications.

Opening Files

func open(URL)

Opens the location at the specified URL.

func open​File(String)

Opens the specified file specified using the default app associated with its type.

func open​File(String, with​Application:​ String?, and​Deactivate:​ Bool)

Opens the specified file and optionally deactivates the sending app.

func open​File(String, from:​ NSImage?, at:​ NSPoint, in:​ NSView?)

Opens a file using the default app for its type and animates the action using a custom icon.

Deprecated

Manipulating Applications

Manipulating Files

func duplicate([URL], completion​Handler:​ (([URL :​ URL], Error?) -> Void)? = nil)

Duplicates the specified URLS asynchronously in the same manner as the Finder.

func recycle([URL], completion​Handler:​ (([URL :​ URL], Error?) -> Void)? = nil)

Moves the specified URLs to the trash in the same manner as the Finder.

func activate​File​Viewer​Selecting([URL])

Activates the Finder, and opens one or more windows selecting the specified files.

Manipulating Uniform Type Identifier Information

func type(of​File:​ String)

Returns the uniform type identifier of the specified file, if it can be determined..

func localized​Description(for​Type:​ String)

Returns the localized description for the specified Uniform Type Identifier

func preferred​Filename​Extension(for​Type:​ String)

Returns the preferred filename extension for the specified Uniform Type Identifier.

func filename​Extension(String, is​Valid​For​Type:​ String)

Returns whether the specified filename extension is appropriate for the Uniform Type Identifier.

func type(String, conforms​To​Type:​ String)

Returns a Boolean indicating that the first Uniform Type Identifier conforms to the second Uniform Type Identifier.

func url​For​Application(with​Bundle​Identifier:​ String)

Returns the URL for the app with the specified identifier.

Managing Icons

func icon(for​File:​ String)

Returns an image containing the icon for the specified file.

func icon(for​File​Type:​ String)

Returns an image containing the icon for files of the specified type.

func icon(for​Files:​ [String])

Returns an image containing the icon for the specified files.

Unmounting a Device

func unmount​And​Eject​Device(at​Path:​ String)

Unmounts and ejects the device at the specified path.

func unmount​And​Eject​Device(at:​ URL)

Attempts to eject the volume mounted at the given path.

Managing the Desktop Image

func desktop​Image​URL(for:​ NSScreen)

Returns the URL for the desktop image for the given screen.

func set​Desktop​Image​URL(URL, for:​ NSScreen, options:​ [String :​ Any] = [:​])

Sets the desktop image for the given screen to the image at the specified URL.

func desktop​Image​Options(for:​ NSScreen)

Returns the desktop image options for the given screen.

Performing Finder Spotlight Searches

func show​Search​Results(for​Query​String:​ String)

Displays a Spotlight search results window in Finder for the specified query string.

Finder File Labels

var file​Label​Colors:​ [NSColor]

Returns the corresponding array of file label colors for the file labels.

var file​Labels:​ [String]

Returns the array of file labels as strings.

Tracking Changes to the File System

func note​File​System​Changed(String)

Informs the workspace object that the file system changed at the specified path.

Requesting Additional Time Before Logout

func extend​Power​Off(by:​ Int)

Requests the system wait for the specified amount of time before turning off the power or logging out the user.

Supporting Accessibility

var accessibility​Display​Should​Differentiate​Without​Color:​ Bool

A Boolean value that indicates whether the app should avoid conveying information through color alone.

var accessibility​Display​Should​Increase​Contrast:​ Bool

A Boolean value that indicates whether the app should present a high contrast user interface.

var accessibility​Display​Should​Reduce​Transparency:​ Bool

A Boolean value that indicates whether the app should avoid using semitransparent backgrounds.

Deprecated Methods

func mounted​Removable​Media()

Returns the full pathnames of all currently mounted removable disks.

Deprecated
func mounted​Local​Volume​Paths()

Returns the mount points of all local volumes, not just the removable ones returned by mounted​Removable​Media().

Deprecated
func active​Application()

Returns a dictionary with information about the current active app.

Deprecated

Constants

Workspace Launch Configuration Options

The following keys can be used in the configuration dictionary of the launch​Application(at:​options:​configuration:​) method. Each key is optional, and if omitted, default behavior is applied.

File Operations

These constants specify different types of file operations used by perform​File​Operation(_:​source:​destination:​files:​tag:​).

Desktop Image Dictionary Keys

The following keys may be specified or returned in the options dictionary for set​Desktop​Image​URL(_:​for:​options:​).

Volume Mounting Notification User Info Keys

The following keys are available in the user​Info parameter of the notification named NSWorkspace​Did​Rename​Volume.

NSWorkspace​Did​Rename​Volume​Notification User Info Keys

The following keys are available in the user​Info parameter of the notification named NSWorkspace​Did​Rename​Volume.

NSWorkspace​Application​Key User Info Key

This constant is supplied in the userInfo dictionary of various notifications.

NSWorkspace​Icon​Creation​Options

These constants describe the NSWorkspace​Icon​Creation​Options values used by set​Icon(_:​for​File:​options:​). You can combine these using the C bitwise OR operator.

Notifications

All NSWorkspace notifications are posted to the NSWorkspace object’s own notification center, not the app’s default notification center. Access this center using the NSWorkspace object’s notification​Center method.

static let NSWorkspace​Did​Rename​Volume:​ NSNotification.Name

Posted when a volume changes its name and/or mount path. These typically change simultaneously, in which case only one notification is posted.

static let NSWorkspace​Did​Mount:​ NSNotification.Name

Posted when a new device has been mounted.

static let NSWorkspace​Will​Unmount:​ NSNotification.Name

Posted when the Finder is about to unmount a device.

static let NSWorkspace​Did​Unmount:​ NSNotification.Name

Posted when the Finder did unmount a device.

static let NSWorkspace​Did​Perform​File​Operation:​ NSNotification.Name

Posted when a file operation has been performed in the receiving app.

Deprecated
static let NSWorkspace​Did​Change​File​Labels:​ NSNotification.Name

Posted when the Finder file labels or colors change.

static let NSWorkspace​Did​Wake:​ NSNotification.Name

Posted when the machine wakes from sleep.

static let NSWorkspace​Will​Power​Off:​ NSNotification.Name

Posted when the user has requested a logout or that the machine be powered off.

static let NSWorkspace​Screens​Did​Sleep:​ NSNotification.Name

Posted when the machine’s screen goes to sleep.

Relationships

Inherits From