NSRunningApplication Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/AppKit.framework |
| Availability | Available in OS X v10.6 and later. |
| Declared in | NSRunningApplication.h |
Overview
NSRunningApplication is a class to manipulate and provide information for a single instance of an application. Only user applications are tracked; this does not provide information about every process on the system.
Some properties of an application are fixed, such as the bundle identifier. Other properties may vary over time, such as whether the app is hidden. Properties that vary can be observed with key-value observing, in which case the description comment for the method notes this capability.
Properties that vary over time are inherently race-prone. For example, a hidden app may unhide itself at any time. To ameliorate this, properties persist until the next turn of the main run loop in a common mode. For example, if you repeatedly poll an unhidden app for its hidden property without allowing the run loop to run, it will continue to return NO, even if the app hides, until the next turn of the run loop.
NSRunningApplication is thread safe, in that its properties are returned atomically. However, it is still subject to the main run loop policy described above. If you access an instance of NSRunningApplication from a background thread, be aware that its time-varying properties may change from under you as the main run loop runs (or not).
An NSRunningApplication instance remains valid after the application exits. However, most properties lose their significance, and some properties may not be available on a terminated application.
To access the list of all running applications, use the runningApplications method in NSWorkspace.
Tasks
Getting Running Application Instances
-
+ runningApplicationWithProcessIdentifier: -
+ runningApplicationsWithBundleIdentifier: -
+ currentApplication
Activating Applications
-
activeproperty -
– activateWithOptions: -
activationPolicyproperty
Hiding and Unhiding Applications
Application Information
-
localizedNameproperty -
iconproperty -
bundleIdentifierproperty -
bundleURLproperty -
executableArchitectureproperty -
executableURLproperty -
launchDateproperty -
finishedLaunchingproperty -
processIdentifierproperty -
ownsMenuBarproperty
Terminating Applications
Properties
activationPolicy
Indicates the activation policy of the application. (read-only)
Discussion
The value returned by this property is usually fixed, but it may change through a call to activateWithOptions:.
This property is observable using key-value observing.
Availability
- Available in OS X v10.6 and later.
Declared In
NSRunningApplication.hactive
Indicates whether the application is currently frontmost. (read-only)
Discussion
This property is observable using key-value observing.
Availability
- Available in OS X v10.6 and later.
Declared In
NSRunningApplication.hbundleIdentifier
Indicates the CFBundleIdentifier of the application. (read-only)
Discussion
The value of this property will be nil if the application does not have an Info.plist.
Availability
- Available in OS X v10.6 and later.
Declared In
NSRunningApplication.hbundleURL
Indicates the URL to the application's bundle. (read-only)
Discussion
The value of this property is nil of the application does not have a bundle structure.
Availability
- Available in OS X v10.6 and later.
See Also
Declared In
NSRunningApplication.hexecutableArchitecture
Indicates the executing processor architecture for the application. (read-only)
Discussion
The returned value will be one of the constants in Mach-O Architecture in NSBundle Class Reference.
Availability
- Available in OS X v10.6 and later.
See Also
Declared In
NSRunningApplication.hexecutableURL
Indicates the URL to the application's executable. (read-only)
Availability
- Available in OS X v10.6 and later.
See Also
Declared In
NSRunningApplication.hfinishedLaunching
Indicates whether the receiver’s process has finished launching, (read-only)
Discussion
The value of this property corresponds to the running application having received an NSApplicationDidFinishLaunchingNotification notification internally. Some applications do not post this notification (applications that do not rely on NSApplication) and so are never reported as finished launching.
This property is observable using key-value observing.
Availability
- Available in OS X v10.6 and later.
Declared In
NSRunningApplication.hhidden
Indicates whether the application is currently hidden. (read-only)
Discussion
This property is observable using key-value observing.
Availability
- Available in OS X v10.6 and later.
Declared In
NSRunningApplication.hicon
Returns the icon for the receiver’s application. (read-only)
Availability
- Available in OS X v10.6 and later.
Declared In
NSRunningApplication.hlaunchDate
Indicates the date when the application was launched. (read-only)
Discussion
This property is only available for applications that were launched by LaunchServices.
Availability
- Available in OS X v10.6 and later.
Declared In
NSRunningApplication.hlocalizedName
Indicates the localized name of the application. (read-only)
Discussion
The value of this property is dependent on the current localization of the application and is suitable for presentation to the user.
Availability
- Available in OS X v10.6 and later.
Declared In
NSRunningApplication.hownsMenuBar
Returns whether the application owns the current menu bar. (read-only)
Discussion
This property is observable using key-value observing.
Availability
- Available in OS X v10.7 and later.
Declared In
NSRunningApplication.hprocessIdentifier
Indicates the process identifier (pid) of the application. (read-only)
Discussion
Not all applications have a pid. Applications without a pid return a value of -1.
Do not rely on this for comparing processes, instead compare NSRunningApplication instances using isEqual:.
Availability
- Available in OS X v10.6 and later.
Declared In
NSRunningApplication.hterminated
Indicates that the receiver’s application has terminated. (read-only)
Discussion
The value of terminated is YES if the receiver’s application has terminated, otherwise NO.
This property is observable using key-value observing.
Availability
- Available in OS X v10.6 and later.
See Also
Declared In
NSRunningApplication.hClass Methods
currentApplication
Returns an NSRunningApplication representing this application.
Return Value
An NSRunningApplication instance for the current application.
Availability
- Available in OS X v10.6 and later.
Declared In
NSRunningApplication.hrunningApplicationsWithBundleIdentifier:
Returns an array of currently running applications with the specified bundle identifier.
Parameters
- bundleIdentifier
The bundle identifier.
Return Value
An array of NSRunningApplications, or an empty array if no applications match the bundle identifier.
Availability
- Available in OS X v10.6 and later.
Declared In
NSRunningApplication.hrunningApplicationWithProcessIdentifier:
Returns the running application with the given process identifier, or nil if no application has that pid.
Parameters
- pid
The process identifier.
Return Value
An instance of NSRunningApplication for the specified pid, or nil if the application has no process identifier.
Discussion
Applications that do not have PIDs cannot be returned from this method.
Availability
- Available in OS X v10.6 and later.
Declared In
NSRunningApplication.hterminateAutomaticallyTerminableApplications
Terminates invisibly running applications as if triggered by system memory pressure.
Discussion
This method is intended for installer applications and similar applications to ensure that nothing is unexpectedly relying on the files they're replacing.
Availability
- Available in OS X v10.7 and later.
Declared In
NSRunningApplication.hInstance Methods
activateWithOptions:
Attempts to activate the application using the specified options.
Parameters
- options
The options to use when activating the application. See “NSApplicationActivationOptions” for the possible values.
Return Value
YES if the application was activated successfully, otherwise NO.
Discussion
This method will return NO if the application has quit, or is not a type of application than can be activated.
Availability
- Available in OS X v10.6 and later.
Declared In
NSRunningApplication.hforceTerminate
Attempts to force the receiver to quit.
Return Value
Returns YES if the application successfully terminated, otherwise NO.
Discussion
This method will return NO if the application is no longer running when the forceTerminate message is sent to the receiver.
This method may return before the receiver exits; you should observe the terminated property to determine when the application terminates.
Availability
- Available in OS X v10.6 and later.
See Also
Declared In
NSRunningApplication.hhide
Attempts to hide or the application.
Return Value
YES if the application was successfully hidden, otherwise NO.
Discussion
The property of this value will be NO if the application has already quit, or if of a type that is unable to be hidden.
Availability
- Available in OS X v10.6 and later.
See Also
Declared In
NSRunningApplication.hterminate
Attempts to quit the receiver normally.
Return Value
Returns YES if the application successfully terminated, otherwise NO.
Discussion
This method will return NO if the application is no longer running when the terminate message is sent to the receiver.
This method may return before the receiver exits; you should observe the terminated property to determine when the application terminates.
Availability
- Available in OS X v10.6 and later.
See Also
Declared In
NSRunningApplication.hunhide
Attempts to unhide or the application.
Return Value
YES if the application was successfully shown, otherwise NO.
Discussion
The property of this value will be NO if the application has already quit, or if of a type that is unable to be hidden.
Availability
- Available in OS X v10.6 and later.
See Also
Declared In
NSRunningApplication.hConstants
NSApplicationActivationOptions
The following flags are for activateWithOptions:.
enum {
NSApplicationActivateAllWindows = 1 << 0,
NSApplicationActivateIgnoringOtherApps = 1 << 1
};
typedef NSUInteger NSApplicationActivationOptions;
Constants
NSApplicationActivateAllWindowsBy default, activation brings only the main and key windows forward. If you specify NSApplicationActivateAllWindows, all of the application's windows are brought forward.
Available in OS X v10.6 and later.
Declared in
NSRunningApplication.h.NSApplicationActivateIgnoringOtherAppsBy default, activation deactivates the calling app (assuming it was active), and then the new app is activated only if there is no currently active application. This prevents the new app from stealing focus from the user, if the app is slow to activate and the user has switched to a different app in the interim. However, if you specify NSApplicationActivateIgnoringOtherApps, the application is activated regardless of the currently active app, potentially stealing focus from the user. You should rarely pass this flag because stealing key focus produces a very bad user experience.
Available in OS X v10.6 and later.
Declared in
NSRunningApplication.h.
NSApplicationActivationPolicy
The following activation policies control whether and how an application may be activated. They are used by activationPolicy.
enum {
NSApplicationActivationPolicyRegular,
NSApplicationActivationPolicyAccessory,
NSApplicationActivationPolicyProhibited
};
typedef NSInteger NSApplicationActivationPolicy;
Constants
NSApplicationActivationPolicyRegularThe application is an ordinary app that appears in the Dock and may have a user interface. This is the default for bundled apps, unless overridden in the
Info.plist.Available in OS X v10.6 and later.
Declared in
NSRunningApplication.h.NSApplicationActivationPolicyAccessoryThe application does not appear in the Dock and does not have a menu bar, but it may be activated programmatically or by clicking on one of its windows. This corresponds to value of the
LSUIElementkey in the application’sInfo.plistbeing1.Available in OS X v10.6 and later.
Declared in
NSRunningApplication.h.NSApplicationActivationPolicyProhibitedThe application does not appear in the Dock and may not create windows or be activated. This corresponds to the value of the
LSBackgroundOnlykey in the application’sInfo.plistbeing1. This is also the default for unbundled executables that do not haveInfo.plists.Available in OS X v10.6 and later.
Declared in
NSRunningApplication.h.
© 2011 Apple Inc. All Rights Reserved. (Last updated: 2011-06-06)