以编程方式确定活动应用程序的 .icns 文件 (OS X)

发布于 2024-11-08 18:53:03 字数 943 浏览 0 评论 0原文

问题摘要:

  1. 如何获取 OS X 下当前活动应用程序的路径? (即拥有当前关键窗口的应用程序)

  2. 由此,我需要确定该应用程序通常关联的 .icns 文件(即当您在“应用程序”文件夹中查看该应用程序时查找器显示的图像,在按下 command(apple) tab 键时出现的应用程序列表中)

详细问题描述:

我正在开发一个OS X实用程序工具栏(浮动窗口),需要显示当前应用程序的名称和图标图像(例如拥有当前为“关键”的窗口

  1. 我需要某种方式来以编程方式确定当前主应用程序的“.app 文件夹的路径”,这可以通过通知回调或其他方式来实现。可以轮询计时器并触发我自己的回调。

  2. 我可以看到,在 XXX.app/Resources 文件夹中手动查找 .icns 文件相对容易,但问题是该文件夹中有时有超过 1 个 .icns 文件 - 而且我看不到简单的方法来确定仅从文件名来看,这是通常与应用程序关联的正确图标 - 虽然许多应用程序使用与应用程序相同的名称(带有 icns 扩展名),但有些则不然。

例如 safari 有一个完全不相关的名称“compass.icns”。一定有一个文件告诉 finder 使用哪个文件 - 或者一个 api 调用来执行此操作,有人有任何想法吗? NSApplication 有 applicationIconImage 方法,它显然返回我的应用程序的图标 - 这不是我想要的 - 据我所知,没有办法为另一个应用程序实例化 NSApplication,并且像大多数方法一样这样做没有任何实际意义/properties 是该应用程序私有的东西。

为了它的价值,我将一个 Windows 实用程序移植到 OS X - 如果我在 Windows 下执行此操作,我将使用 GetWindowProcessID(GetForegroundWindow)),并使用它来确定应用程序的路径,并使用从中提取图标加载图标

Question Summary:

  1. how can I get the path to the currently active application under OS X? (ie the application that owns the current key window)

  2. from that, I need to determine the normally associated .icns file for that application (ie the image that finder shows when you look at that application in the "Applications" folder, and in the application list that appears when you press command(apple) tab key)

Detailed Question description:

I am developing an OS X utility toolbar (floating window) that needs to display the name and icon image for the current application (eg the application that owns the window that is currently "key".

  1. I need some way of programmatically determining "the path to .app folder" of the application that is currently main & key. This could either by via a notification callback, or something i can poll on a timer and trigger my own callbacks.

  2. I can see that manually finding .icns files inside the XXX.app/Resources folder is relatively easy, however the problems is there are sometimes more than 1 .icns file in that folder - and I can see no easy way to determine from the filename alone which is the correct icon normally associated with the application - whilst many applications use the same same name as the application (with an icns extension),some don't.

safari for example has a completely unrelated name "compass.icns". there must be a file somewhere that tells finder what file to use - or an api call to do this, anyone have any ideas? NSApplication has the applicationIconImage method, which obviously returns the icon for MY application - that's not what I want - as far as I know there is no way of instantiating NSApplication for another application, and it would make no real sense to do this as most methods/properties are things that are private to that application.

for what it's worth, I am porting a windows utility to OS X - if I were doing this under windows, I would use GetWindowProcessID(GetForegroundWindow)), and use that to determine the path to the application, and extract the icon from that using LoadIcon

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

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

发布评论

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

评论(2

深空失忆 2024-11-15 18:53:03

所有这些方法都位于 <代码>NSWorkspace类。您可以获得活动应用程序及其图标图像(没有 icns 文件)。检查上述类的文档,您应该很清楚:

NSDictionary *activeAppDict = [[NSWorkspace sharedWorkspace] activeApplication];
NSString *activeApplicationPath = [activeAppDict valueForKey:@"NSApplicationPath"];
NSImage *activeAppImage = [[NSWorkspace sharedWorkspace] iconForFile:activeApplicationPath];

All these methods reside in NSWorkspace class. You can get the active application as well as its icon image (without icns file). Check the documentation for aforementioned class and it all should be clear to you:

NSDictionary *activeAppDict = [[NSWorkspace sharedWorkspace] activeApplication];
NSString *activeApplicationPath = [activeAppDict valueForKey:@"NSApplicationPath"];
NSImage *activeAppImage = [[NSWorkspace sharedWorkspace] iconForFile:activeApplicationPath];
樱娆 2024-11-15 18:53:03

您应该能够使用以下方法将您想要的内容串起来:

  • -[NSWorkspace runningApplications]
  • -[NSRunningApplication active]
  • -[NSRunningApplication bundleURL]
  • -[NSWorkspace iconForFile:]

首先通过循环运行的应用程序获取最前面的应用程序,然后选择活跃的*。获取 NSRunningApplication 的bundleURL,将其转换为路径,并将其传递给 -[NSWorkspace iconForFile:]。现在您已经获得了最前面应用程序的 NSImage。

*文档说活动应用程序是最前面的应用程序,但我认为它确实意味着关键,因为我不希望它为最前面但不是关键的应用程序返回 YES。

You should be able to string what you want together with these methods:

  • -[NSWorkspace runningApplications]
  • -[NSRunningApplication active]
  • -[NSRunningApplication bundleURL]
  • -[NSWorkspace iconForFile:]

First get the front most application by looping over the running applications, and choosing the active one*. The get the bundleURL for the NSRunningApplication, turn it into a path, and pass it to -[NSWorkspace iconForFile:]. Now you've got an NSImage for the front most application.

*The documentation says that active application is the front most one, but I think it really means key as I wouldn't expect it to return YES for an application that was front most but not key.

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