4

Let's say I have some tool that, at some point in its execution, asks for user input. For example, it might ask for name and address. At another point it might ask for a password (and retyping of the password).

Is it possible for NSTask and NSPipe objects to deal with these things, i.e. to interact with command line tools?

Enchilada
  • 3,859
  • 1
  • 36
  • 69

1 Answers1

4

See the setStandardInput: method of NSTask. It allows you to set either a NSPipe or NSFileHandle as the task's standard input before launching it. There are also similar methods for standard output and standard error.

ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123
  • 2
    Note that it may not always be possible to use `NSTask` to provide input. For example, a command-line tool may use ncurses input functions that do not go through `stdin`, or it may read directly from `/dev/tty` instead of using `stdin`. This is potentially true for tools that ask for passwords. –  Jun 06 '11 at 21:22
  • I am aware of these classes and methods. However, they confuse me, and I'm not sure how to use them in this case. For instance, let's say a tool will first do some processing (say compress a file) and then ask me for a password, and then a repetition of the password. Should I just pass in the string @"myPassword\nmyPassword" (note the line separation) to pass in the password and the repeated password? Is it that simple? It's this practical aspect that I'm asking for. – Enchilada Jun 06 '11 at 22:52
  • 1
    @Enchilada If the tool uses standard input, and doesn't expect anything else, then yes. However, keep Bavarious's comment in mind. Some tools might not use stdin, and you will have to find another way. – ughoavgfhw Jun 07 '11 at 01:01