views:

305

answers:

2

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.

+3  A: 

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)];
Georg
ok, sorry if I don't get it... Like my code that opens an app from just the name, how would something similar like that work? just the terminating. Cuz i don't want to add the path to the application that needs to be closed..
Kevin
my new example should be fully functional
Georg
ok i get this erro that NSRunningApplication may not respond to +runningApplicationWithBundleIdentifier
Kevin
fixed it, I should have paid more attention to the api.
Georg
Ok, so I don't get any errors but when i type in the name of the app such as safari, in my textbox which is (appNAme) and press enter (to quit) it just quits my program, instead of safari.
Kevin
Have you figured out what the problem was?
Georg
+5  A: 

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.

Perspx
appname is an NSSTring..
Kevin
Yes, that was what I was saying – you need to create an `NSRunningApplication` instance using one of the above methods.
Perspx
ok i have this:NSString *identifier = [[NSBundle bundleWithPath:appName] bundleIdentifier]; NSRunningApplication *selectedApp = [NSRunningApplication runningApplicationWithBundleIdentifier:identifier]; [selectedApp terminate];dis still doesn't work,...
Kevin
Is `appName` a full path? You have to pass a full path to `bundleWithPath:` for it to return an `NSBundle` instance which you can then call `bundleIdentifier` on. If the path isn't full (or isn't a valid bundle) then it will return `nil`.
Perspx