Question 开发者_开发问答Summary:
how can I get the path to the currently active application under OS X? (ie the application that owns the current key window)
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".
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.
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
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];
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.
精彩评论