Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the executable path of a command line tool in Objective C (Foundation framework)?

Tags:

objective-c

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.

like image 453
Riaz Avatar asked Dec 01 '22 06:12

Riaz


2 Answers

Call me a purist - or a bundle-hater - if you must.. but I "like"

NSString *myLittleCLIToolPath = NSProcessInfo.processInfo.arguments[0];

like image 173
Alex Gray Avatar answered Dec 05 '22 10:12

Alex Gray


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];
like image 36
mipadi Avatar answered Dec 05 '22 10:12

mipadi