gpt4 book ai didi

cocoa - Mac 应用程序之间的 API 切换?

转载 作者:行者123 更新时间:2023-12-03 17:25:24 24 4
gpt4 key购买 nike

我正在寻找 Cocoa/Carbon API 来使我能够在不同的应用程序之间切换?

类似于按下 Command + Tab 时的情况。

我不需要 NSWorkspace 启动应用程序。我知道这将使应用程序成为焦点,并且我可以在应用程序之间切换。我想做系统做的事情。

谢谢!

最佳答案

在 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]];

关于cocoa - Mac 应用程序之间的 API 切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6709270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com