cocoa -- determine directory from which a tool was launched? - cocoa

I have a command-line tool written in Cocoa. Let's call it processFile. So if I am in the terminal and I type in the command ./processFile foo, it looks for a file named foo.html in the same directory as the executable of processFile. If it finds one, it reads it and does some stuff to create fooProcessed.html.
Now I want to modify my tool so that it looks for foo.html in the directory from which it was launched. So if I am in the terminal with current directory ~/documents/html, and processFile executable is in usr/bin, and I type in
processFile foo
it will find and process the file ~/documents/foo.html.
The problem is that I don't know how to get the directory from which the tool was invoked. How can I do that?

That's the current working directory. First of all, any attempt to access the file just using its name and no path will automatically use the working directory. So, if you simply take "foo", append ".html", and attempt to open the file, that will work. If the user specified a relative path, like "subdir/foo", that would also work. It would resolve the relative path starting from the current working directory.
You can also query the working directory using the getcwd() routine.

Related

Executing a bash script from anywhere on Windows

I am on Windows.
I have a script file named basics.sh and here is what it contains:
cd opt-out-exam/abduvosid_malikov/IT
mkdir made_by_my_script
cd made_by_my_script
echo "Hello World" > hello.txt
so basically, basics.sh script file is responsible to:
go to folder opt-out-exam/abduvosid_malikov/IT
make a directory made_by_my_script
create hello.txt file with content Hello World
Right now. to execute this basics.sh script, I am going to IT folder and writing this command in the terminal:
./basics.sh
In order to execute this basics.sh script, is it compulsory for me to go to IT folder
OR
is it possible to execute this script file even if I am staying in another folder (lets say currently working directory is opt-out-exam)
The first line is a change directory command followed by a relative path, not absolute. In such cases, it is important where you run the script. (An absolute path would start with the filesystem root, i. e. /.)
If you run this script from a directory (I wouldn't call it a folder in this context) where the relative path opt-out-exam/abduvosid_malikov/IT does not exist, it won't cd into it. But it will make a new directory without any problem, it will also create the file and write a line into it.
So only the first line will fail if it's run somewhere else.
UPD: As Gordon Davisson pointed out, this means that you want to check whether the directory change actually took place or not.

Script runs from command line, but not from finder

I have a bash script that runs my Go program. That's all it does, and when I run it from the command line, it works fine.
But when I run it by double clicking on it in Finder, it returns
/Users/colin/go/metgen/metaphorgenerator.sh: line 2: ./binary: No such file or directory
So I made it echo it's working directory, and it just prints /Users/colin, my home directory.
How do I get it to run the code from the directory the file is in? (I want it to work no matter what directory it's in)
You need to point it to the location of the executable, either with a relative path from the working directory as in go/metgen/binary, absolute path like /Users/colin/go/metgen/binary, or absolute path based on the parent executable (unfortunately not reliable).
$0 is the full program name. So you can get the directory with HERE=$(dirname "$0").
Then, line 2 should have ${HERE}/binary.

Refer to a directory in your working directory

I'm working on the Mac Terminal with the program called QIIME. However, my question is more related to basic navigating in the terminal.
When I enter a command, and would like to refer to a file that is located in a directory/map inside my current working directory, how do I do this?
For example:
convert_fastaqual_fastq.py -f sequenceA.fastq
Now sequenceA is located in a map in my working directory, so I guess I'll have to add arguments before sequenceA.qual, or shouldn't I ?
from your working directory you should be able to access that file with:
the_directory_your_file_is_in/sequenceA.qual
else, you could use something like:
./the_directory_your_file_is_in/sequenceA.qual
A point stands for the directory your are currently in.
../the-directory-you-are-in/the_directory_your_file_is_in/sequenceA.qual
Two points stand for the directory above your current directory.

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
Use NSFileManager's currentDirectoryPath method:
NSString *currentpath = [[NSFileManager defaultManager] currentDirectoryPath];
How does cp know the absolute path of filename.txt?
It does not know it and doesn't have to. It can just use the relative path. The current working directory is inherited from the parent process.
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.
What do you mean? The directory where the executable is in? That's usually not of much interest. The only other directory that plays a role in this context is the current working directory. If you already got that, what else do you need?

Relative file paths

I am trying to read in from a few files using something like this
IO.foreach("TeamFields.txt") { |line| fieldNames.push(line.chomp) }
It works fine when running the from the command line, but when I package to an .exe with shoes and run it can't find the file. Is there a way to specify a path relative the the .exe or do I have to provide the full filepath (e.g. "c:\files\TeamFields.txt")? Thanks for the help.
This is because your executable is not run with the correct current directory.
Either fix the current directory (for example in the shortcut) or modify your Ruby program to automatically set the working directory to the program directory with:
Dir.chdir(File.dirname($PROGRAM_NAME))
You need to set "Current Application Directory" correctly before going relative.
The user can execute your app with different start up dir, or system can call your app with different dir.
If files in question are in the folder of your app, the only thing you need to do is to get that folder, and set it to be current.
I don't program in ruby, but I do with windows, and odds are the relative path will be based on the location of the .exe file.
So, yes, you're probably better off passing a full path for the file name.
The constant __FILE__ will contain the full path to the currently executing file. You can then use methods of the File class to strip off the filename, append the relative path for whatever other file in your package it is you want and resolve the result.

Resources