views:

273

answers:

2

Hey Everyone,

I have this code:

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

which is suppose to close any application running from just the name which is: appName (NSString).

When I debug the app and type in the application name into the NSTextField pointing towards appName, it closes my application instead of the other application I want it to terminate. I replied this question on another post but no one is responding so I thought maybe I can get a response if I start a new post... thanks. (THIS IS NOT A DUPLICATE, its just that people don't respond when I reply bakc...)

Thanks.,

Kevin

+1  A: 

Check the Bundle Identifier of your app in the Info.plist of your project and make sure it's unique.

Also, you should determine the value of selectedApps that you're actually passing as an argument. To do this either log it:

NSLog(@"selectedApps: %@", selectedApps);

or (and this is suggested in the comments by Jon Hess), create a breakpoint. You can do this a few ways:

Once you've set the breakpoint, choose Run->Debug. This will execute your program in the debugger (gdb). Do whatever you normally do to get to the point of failure. However, this time instead of terminating anything, it will stop at the line you specified. at this point you can examine your variables. You can print objective-c instances by issuing a 'po' (print object) command. Thus, you might end up with something like:

(gdb)po appPath
    // gdb will print this
(gdb)po identifier
    // gdb will print this
(gdb)po selectedApps
    // gdb will print this
nall
Rather than using a log statement, you should instead take the time to understand how to use the debugger. One method would be to put a break point on the "[selectedApps makeObjectsPerformSelector:@selector(terminate)]" line and then type "po selectedApps" in the debugger console.
Jon Hess
Okay I really don't know what is going wrong...
Kevin
Could you give some more information? What is your bundle indentifier? What was the value of selectedApps before the call to terminate?
nall
Ok I can give you the bundle identifier from my info plist which is:com.cityconnection.${Helios:rfc1034identifier}I'm not sure about the selectedApps. I don't add a value to it in the beginning. I call the variable selectedapps in the code above...
Kevin
Kevin: Use the debugger to print it.
Peter Hosey
how would I do this?
Kevin
Kevin, I updated this answer to describe the process.
nall
Actually, I get "Debugging of "APP NAME" ended normally" IT should actually terminate another application that is running...
Kevin
It should never even get to that point if you've enabled the breakpoint and are running under gdb. It sounds like you're either not executing the code you think you're executing or you didn't setup the breakpoint correctly.
nall
sorry, I found the problem! I haad another command with the same instance as quit, and that instance quits the application. SORRY!
Kevin
A: 

And you're not entering your app's own name, right?

Peter Hosey
No, I'm putting in the the other application I want to close.
Kevin