Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a terminal window to a specified folder from a Cocoa app

I have seen this thread on how to execute terminal commands from within a Cocoa app. But I want to actually launch Terminal.app to a specified directory.

I know that the following does not work:

[[NSWorkspace sharedWorkspace] openFile:folderPath withApplication:@"Terminal"];

Terminal tries to actually open the folder as a file.

Is this something I have to use AppleScript for?

Any ideas?

like image 856
Corey Floyd Avatar asked Sep 18 '09 20:09

Corey Floyd


People also ask

How do I go to a specific directory in terminal?

cd or change directory The cd command allows you to move between directories. The cd command takes an argument, usually the name of the folder you want to move to, so the full command is cd your-directory . Now that we moved to your Desktop, you can type ls again, then cd into it.

How do I open a terminal in a specific folder Mac?

Open the parent directory where your folder is located. Then single-click on the folder where you wish to launch a Terminal window, click on “Finder” followed by “Services,” and select “New Terminal at Folder.” Or you can simply press the keyboard shortcut that you assigned before.

How do I change directories in terminal?

To change this current working directory, you can use the "cd" command (where "cd" stands for "change directory"). For example, to move one directory upwards (into the current folder's parent folder), you can just call: $ cd ..


1 Answers

You could use AppleScript from Cocoa like this:

NSString *s = [NSString stringWithFormat:
     @"tell application \"Terminal\" to do script \"cd %@\"", folderPath];

NSAppleScript *as = [[NSAppleScript alloc] initWithSource: s];
[as executeAndReturnError:nil];

AppleScript script was taken from cobbal. Thanks mate!

like image 184
Woofy Avatar answered Oct 02 '22 21:10

Woofy