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?
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.
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.
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 ..
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With