Class

NSWorkspace

A workspace that can launch other apps and perform a variety of file-handling services.

Declaration

class NSWorkspace : NSObject

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.shared.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 activeApplication() and launchedApplications, and is also provided in the userInfo of NSWorkspace notifications for app launch and termination.

Note that these constants are considered legacy.

Table 1

Dictionary keys for launched and active apps

Key

Value

@"NSApplicationPath"

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

@"NSApplicationName"

The app's name, as an NSString object.

@"NSApplicationBundleIdentifier"

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

@"NSApplicationProcessIdentifier"

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

@"NSApplicationProcessSerialNumberHigh"

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

@"NSApplicationProcessSerialNumberLow"

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

Topics

Accessing the Shared Workspace

class var shared: NSWorkspace

The shared workspace object.

Accessing the Workspace Notification Center

var notificationCenter: NotificationCenter

The notification center for workspace notifications.

Launching and Hiding Apps

func hideOtherApplications()

Hides all applications other than the sender.

Manipulating Files

func duplicate([URL], completionHandler: (([URL : URL], Error?) -> Void)?)

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

func recycle([URL], completionHandler: (([URL : URL], Error?) -> Void)?)

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

func activateFileViewerSelecting([URL])

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

Manipulating Uniform Type Identifier Information

func type(ofFile: String) -> String

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

func localizedDescription(forType: String) -> String?

Returns the localized description for the specified Uniform Type Identifier (UTI).

func preferredFilenameExtension(forType: String) -> String?

Returns the preferred filename extension for the specified Uniform Type Identifier (UTI).

func filenameExtension(String, isValidForType: String) -> Bool

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

func type(String, conformsToType: String) -> Bool

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

func urlForApplication(withBundleIdentifier: String) -> URL?

Returns the URL for the app with the specified identifier.

Managing Icons

func icon(forFile: String) -> NSImage

Returns an image containing the icon for the specified file.

func icon(forFileType: String) -> NSImage

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

func icon(forFiles: [String]) -> NSImage?

Returns an image containing the icon for the specified files.

func setIcon(NSImage?, forFile: String, options: NSWorkspace.IconCreationOptions) -> Bool

Sets the icon for the file or directory at the specified path.

struct NSWorkspace.IconCreationOptions

Constants that describe options for creating icons.

Unmounting a Device

func unmountAndEjectDevice(atPath: String) -> Bool

Unmounts and ejects the device at the specified path.

func unmountAndEjectDevice(at: URL)

Attempts to eject the volume mounted at the given path.

Managing the Desktop Image

func desktopImageURL(for: NSScreen) -> URL?

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

func setDesktopImageURL(URL, for: NSScreen, options: [NSWorkspace.DesktopImageOptionKey : Any])

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

struct NSWorkspace.DesktopImageOptionKey

Keys that indicate how to display a new desktop image.

Performing Finder Spotlight Searches

func showSearchResults(forQueryString: String) -> Bool

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

Finder File Labels

var fileLabels: [String]

The array of file labels, returned as strings.

var fileLabelColors: [NSColor]

The array of colors for the file labels.

Tracking Changes to the File System

func noteFileSystemChanged(String)

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

Requesting Additional Time Before Logout

func extendPowerOff(by: Int) -> Int

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

Supporting Accessibility

var accessibilityDisplayShouldDifferentiateWithoutColor: Bool

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

var accessibilityDisplayShouldIncreaseContrast: Bool

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

var accessibilityDisplayShouldReduceTransparency: Bool

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

Performing Privileged Operations

class NSWorkspace.Authorization

The authorization granted to the app by the user.

enum NSWorkspace.AuthorizationType

The types of privileged file operations that can be authorized by the user.

Responding to Environment Notifications

The workspace posts notifications to its own notification center, not the app’s default notification center.

class let willLaunchApplicationNotification: NSNotification.Name

A notification that the workspace posts when the Finder is about to launch an app.

class let didLaunchApplicationNotification: NSNotification.Name

A notification that the workspace posts when a new app has started up.

class let didTerminateApplicationNotification: NSNotification.Name

A notification that the workspace posts when an app finishes executing.

class let sessionDidBecomeActiveNotification: NSNotification.Name

A notification that the workspace posts after a user session is switched in.

class let sessionDidResignActiveNotification: NSNotification.Name

A notification that the workspace posts before a user session is switched out.

class let didHideApplicationNotification: NSNotification.Name

A notification that the workspace posts when the Finder hid an app.

class let didUnhideApplicationNotification: NSNotification.Name

A notification that the workspace posts when the Finder unhid an app.

class let didActivateApplicationNotification: NSNotification.Name

A notification that the workspace posts when the Finder is about to activate an app.

class let didDeactivateApplicationNotification: NSNotification.Name

A notification that the workspace posts when the Finder deactivated an app.

class let didRenameVolumeNotification: NSNotification.Name

A notification that the workspace posts when a volume changes its name or mount path.

class let didMountNotification: NSNotification.Name

A notification that the workspace posts when a new device has been mounted.

class let willUnmountNotification: NSNotification.Name

A notification that the workspace posts when the Finder is about to unmount a device.

class let didUnmountNotification: NSNotification.Name

A notification that the workspace posts when the Finder unmounts a device.

class let didChangeFileLabelsNotification: NSNotification.Name

A notification that the workspace posts when the Finder file labels or colors change.

class let activeSpaceDidChangeNotification: NSNotification.Name

A notification that the workspace posts when a Spaces change occurs.

class let didWakeNotification: NSNotification.Name

A notification that the workspace posts when the machine wakes from sleep.

class let willPowerOffNotification: NSNotification.Name

A notification that the workspace posts when the user requests a logout or powers off the machine.

class let willSleepNotification: NSNotification.Name

A notification that the workspace posts before the machine goes to sleep.

class let screensDidSleepNotification: NSNotification.Name

A notification that the workspace posts when the machine’s screen goes to sleep.

class let screensDidWakeNotification: NSNotification.Name

A notification that the workspace posts when the machine’s screens wake.

class let accessibilityDisplayOptionsDidChangeNotification: NSNotification.Name

A notification that the workspace posts when any of the accessibility display options change.

class let localizedVolumeNameUserInfoKey: String

A string containing the user-visible name of the volume.

class let volumeURLUserInfoKey: String

A URL containing the mount path of the volume.

class let oldLocalizedVolumeNameUserInfoKey: String

A string containing the old user-visible name of the volume

class let oldVolumeURLUserInfoKey: String

A URL containing the old mount path of the volume

Deprecated

Deprecated Symbols

Review unsupported symbols and their replacements.

Classes

class NSWorkspace.OpenConfiguration

The configuration options to use when opening URLs or launching apps.

Relationships

Inherits From

Conforms To

See Also

Environment

class NSWorkspace.OpenConfiguration

The configuration options to use when opening URLs or launching apps.

struct NSAppKitVersion

Constants for determining which version of AppKit is available.