Michael
Michael

Reputation: 3628

OS X GUI Cocoa app interacting with shell

Background:

We are developing an in house use only cocoa app to help us with some basic sysadmin tasks and complex website deployments. This application is basically a wrapper around many different bash shell commands. The output from these commands will sometimes need to parsed or displayed to the user.

We have played around with NSTask and are able to launch some scripts. However, this seems somewhat cumbersome (needing to set the exact path, passing in each argument separately, etc...). We can use NSTask in this way if this is actually the preferred method.

The biggest challenge so far is setting up the shell environment. We interact with many ruby gems and programs and deploy using capistrano.

The Question is:

How can we interact with the shell with an environment setup like a user? (aliases, rvm, ruby, paths)

Upvotes: 2

Views: 321

Answers (1)

Michael Dautermann
Michael Dautermann

Reputation: 89509

The environment variables you'll see set in your shell in Terminal.app are pretty much confined to whatever shell you're using in the Terminal context. It's not picked up by "NSTask" automagically.

But... you do have options. Some of them are described in answers to this related question.

You can set more universal environment variables that do get picked up by NSTask via the "~/.launchd.conf" file, or you can set the shell of "NSTask" to match the one in Terminal (which means you pick up .bashrc or .profile or whatever initializes paths) via "[NSTask setLaunchPath:]" (where the launch path is your shell).

And of course you can also call "system()" from within your tools. This may also pick up the variables set in the "~/.launchd.conf" file.

More information on environment variables is available in this question.

Upvotes: 1

Related Questions