tags:

views:

42

answers:

2

Hi, I'm trying to work out a way to identify the executable path of a command line tool in Objective C.

Hence, if the executable is /Applications/Utils/MyTool, then that method would return /Applications/Utils

I'm using the Foundation framework.

Any help would be great, thanks

A: 

You probably want NSString's stringByDeletingLastPathComponent.

tedge
Delete last path component 'from what'? It's the 'what' I'm after!
Riaz
+1  A: 

I'm assuming that by /Applications/Utils/MyTool, you mean an application named "MyTool" in the "Utils" directory within the "Application" directory (which is actually the path /Applications/Utils/MyTools.app). In that case, you could get the directory in which the application resides (/Applications/Utils) with the following bit of code:

NSString *appParentDirectory = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
mipadi
Correct! For some weird reason, I decided to assume NSBundle was under the Cocoa Framework. Thanks
Riaz
It seems like `NSBundle` *should* be in AppKit, since command-line apps normally don't have bundles; but as you have obviously found out, a command-line app *can* have a bundle, so I guess that's why `NSBundle` is part of Foundation kit instead.
mipadi