Search code examples
cocoabashpathcommand-line-tool

How can I get my Cocoa command line tool to know the working path that it was called from in the terminal?


The goal of my cocoa command line program is to take an argument which is the name of a file and import it into the program for parsing. I want to be able to call my program lets say, "ParseFile" from the terminal in mac os, with the argument "Filename.txt"(example$ ParseFile filename.txt), but so far I cant understand how my program can know the absolute path to Filename.txt.

For instance when you use 'cp filename.txt /whateverfolder/filename2.txt' you are copying the file 'filename.txt' from the current working directory of the terminal to a folder called whateverfolder on the root directory. How does cp know the absolute path of filename.txt? if i had filename.txt on my desktop cp would know that i sent it /Users/username/Desktop/filename.txt. This is what I want my program to know.

I have been able to get my program to know where it is executed in, but this is very different from where the current working directory of the bash terminal was at the time of being called.

How do i solve this? Thanks a lot


Solution

  • Use NSFileManager's currentDirectoryPath method:

    NSString *currentpath = [[NSFileManager defaultManager] currentDirectoryPath];