我正在寻找Cocoa / Carbon API以使我能够在不同的应用程序之间切换?
按下Command + Tab时的情况。
我不需要NSWorkspace启动应用程序。我知道这将使应用程序成为焦点,我可以在应用程序之间切换。我想做一些系统做的事情。
谢谢!
答案 0 :(得分:0)
在NSWorkspace中尝试这些方法:
/* The following methods return information about an application as a dictionary containing as many of the following keys as are available:
NSApplicationPath (the full path to the application, as a string)
NSApplicationName (the application's name, as a string)
NSApplicationBundleIdentifier (the application's bundle identifier, as a string)
NSApplicationProcessIdentifier (the application's process id, as an NSNumber)
NSApplicationProcessSerialNumberHigh (the high long of the PSN, as an NSNumber)
NSApplicationProcessSerialNumberLow (the low long of the PSN, as an NSNumber)
The same information will now be provided in the userInfo of the NSWorkspace notifications for application launch and termination.
*/
/* Gets an array of NSDictionaries with the above keys. In addition, the NSWorkspaceApplicationKey is provided, and vends an instance of NSRunningApplication. This method does not return applications that are UIElement or BackgroundOnly. To access the entire list of running applications, use the -[NSWorkspace runningApplications] method, declared in NSRunningApplication.h. */
- (NSArray *)launchedApplications;
/* Launches an application. The appName may be a full path to the app, or the name alone, with or without the .app extension. */
- (BOOL)launchApplication:(NSString *)appName;
/* Launches the app at the given URL. If the app is successfully launched, a reference to the new running app is returned. If the app is already running, and NSWorkspaceLaunchNewInstance is not specified, then a reference to the existing app is returned. If the app could not be launched, nil is returned and an NSError is returned by reference.
The configuration dictionary can be used to pass additional options to the app. Possible keys are listed later in this file (search for NSWorkspaceLaunchConfiguration). The configuration dictionary may be nil, in which case default behavior applies.
*/
- (NSRunningApplication *)launchApplicationAtURL:(NSURL *)url options:(NSWorkspaceLaunchOptions)options configuration:(NSDictionary *)configuration error:(NSError **)error AVAILABLE_MAC_OS_X_VERSION_10_6_AND_LATER;
只需将从第一种方法获得的名称或网址(从路径创建)分别传递给第二种或第三种方法。
NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
[[NSWorkspace sharedWorkspace] launchApplication:[[apps objectAtIndex:0] objectForKey:NSApplicationName]];