API переключения между приложениями Mac? - PullRequest
1 голос
/ 15 июля 2011

Я ищу API какао / углерода, чтобы можно было переключаться между различными приложениями?

Что-то похожее на нажатие Command + Tab.

Мне не нужно запускать приложение NSWorkspace. Я знаю, что это привлечет внимание к приложению, и я могу переключаться между приложениями. Я хочу сделать то, что делает система.

Спасибо!

1 Ответ

0 голосов
/ 15 июля 2011

Попробуйте эти методы в 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;

Просто передайте имя или URL-адрес (созданный из пути), полученный из первого метода, во второй или третий соответственно.

NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
[[NSWorkspace sharedWorkspace] launchApplication:[[apps objectAtIndex:0] objectForKey:NSApplicationName]];
...