NSRunningApplication - 终止

发布于 2024-08-06 18:38:30 字数 329 浏览 6 评论 0原文

我将如何使用NSRunningApplication?我有与启动应用程序相反的东西:

[[NSWorkspace sharedWorkspace] launchApplication:appName];

但我想关闭一个。当我调试 NSRunningApp 的代码时,出现错误:

NSRunningApplication *selectedApp = appName;
[selectedApp terminate];

有什么问题吗?如果有请指出并如何解决。

How would I use NSRunningApplication? I have something opposite of that which is launching an app:

[[NSWorkspace sharedWorkspace] launchApplication:appName];

but I want to close one. I get an error when I debug the code for NSRunningApp which is this:

NSRunningApplication *selectedApp = appName;
[selectedApp terminate];

Is there something wrong? if there is please point it out and how to fix it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

追星践月 2024-08-13 18:38:30

您为变量 selectedApp 分配一个 NSString。字符串没有 - (void)terminate 方法,因此失败。您必须获取指向该应用程序的 NSRunningApplication 实例。

NSWorkspace *sharedWorkspace = [NSWorkspace sharedWorkspace];
NSString *appPath = [sharedWorkspace fullPathForApplication:appName];
NSString *identifier = [[NSBundle bundleWithPath:appPath] bundleIdentifier];
NSArray *selectedApps =
       [NSRunningApplication runningApplicationsWithBundleIdentifier:identifier];
// quit all
[selectedApps makeObjectsPerformSelector:@selector(terminate)];

You assign the variable selectedApp a NSString. Strings don't have the - (void)terminate method and therefore it fails. You have to get a NSRunningApplication instance pointing to the application.

NSWorkspace *sharedWorkspace = [NSWorkspace sharedWorkspace];
NSString *appPath = [sharedWorkspace fullPathForApplication:appName];
NSString *identifier = [[NSBundle bundleWithPath:appPath] bundleIdentifier];
NSArray *selectedApps =
       [NSRunningApplication runningApplicationsWithBundleIdentifier:identifier];
// quit all
[selectedApps makeObjectsPerformSelector:@selector(terminate)];
千纸鹤带着心事 2024-08-13 18:38:30

appName指的是什么?如果它字面上指的是 NSString 那么这是行不通的。

由于 NSRunningApplication 是一个类,因此您必须创建一个实例来向其发送实例方法,就像处理任何其他类一样。

共有三个类方法(请参阅文档< /a>),您可以用来返回一个 NSRunningApplication 实例:

+ runningApplicationWithProcessIdentifier:
+ runningApplicationsWithBundleIdentifier:
+ currentApplication

除非您想要一个基于当前应用程序的 NSRunningApplication 实例,否则您可能会发现前两个类方法最有用。

然后,您可以将 terminate 消息发送到 NSRunningApplication 实例,该实例将尝试退出为其配置的应用程序。

What does appName refer to? If it literally refers to an NSString then that will not work.

Since NSRunningApplication is a class, you have to create an instance to send it an instance method as you would with any other class.

There are three class methods (see the docs) you can use to return an NSRunningApplication instance:

+ runningApplicationWithProcessIdentifier:
+ runningApplicationsWithBundleIdentifier:
+ currentApplication

Unless you want an NSRunningApplication instance based on the current application you are likely to find the first two class methods most useful.

You can then send the terminate message to the NSRunningApplication instance, which will attempt to quit the application that it has been configured for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文