NSTask Returning Null

Hi All,
I’ve been playing around with NSTasks. I’ve set up a handler, pretty much a copy of Shane’s explanation using the /bin/sh shell, and I’m getting a strange result. The task runs a binary called mediainfo that prints out information about video files, and works nearly flawlessly. The exception is that for one video file, the handler returns nothing. More specifically, the standard output is null, and the error output is nothing. The termination status is fine. I tried changing the name of the file, working with copies of the file, with the same result. When I run the command in terminal using the command string that I logged, I get the output I expected. Using do shell script with the exact same string yields the expected output. It just won’t work in NSTask, which makes me wonder if this is something to do with NSPipe. I’ve read that sometimes the output pipe can be overwhelmed, but we’re only talking about 8 pages of text. Is that too much? Any ideas?

The only thing I can suggest is that it’s unlikely to be a pipe problem but the sound of it. Not a big help, I know…

Well I narrowed down the problem. It’s the NSUTF8StringEncoding used when reading the output data into an NSString. I’m not sure why, but the output for this one file apparently uses a different type of encoding. I’m not sure what kind. To be honest this is moving into territory that’s a bit fuzzy for me. I’ve never really worried about this before. My impression has been the your pretty much safe if you use utf8. Is there a way of determining what the data encoding is? Is there perhaps a UNIX command that I could pipe the results through to correct the encoding before being sent to the standardOutput pipe?

The first step is to try to work out what the encoding is. Try NSASCIIStringEncoding and see what you get.

NSASCIIStringEncoding worked. It seems to even work with files with unicode text (asian characters). That doesn’t make sense to me. I also don’t understand why NSASCIIStringEncoding would work and not NSUTF8StringEncoding. Isn’t ASCII and subset of UTF8?

NSASCIIStringEncoding always “works” in that it just grabs the first seven bits of each byte. NSUTF8StringEncoding fails if the result is invalid UTF8, which can happen easily enough. It’s probably using neither…

OK. I guess I could test the result using NSUTF8StringEncoding first, and then if NULL is returned, use NSASCIIStringEncoding. Or is there some better way of handling this? I’ve recently discovered NSString’s stringWithContentsOfURL:encoding:error: and it seems that you could run into the same issue.

Also, as a side question related to NSString’s stringWithContentsOfURL:encoding:error: . Before discovering this, I’d use do shell script “cat myFile” to read the contents of myFile, and I never had to worry about the encoding. In fact, I could read the above problem file using this method. Any ideas on why that is, and can I somehow duplicate this with NSTask.

From what I’ve read, stringWithContentsOfURL:encoding:error: is not very reliable.

I’ve seen sample code for reading files that typically tries UTF8 and drops back to MacRoman if it fails – not what you want, but a similar approach to what you’re describing.

It depends a lot on what you’re reading. The sort of stuff a lot of CLI tools put out is going to be the same pretty much whatever encoding you use.

I suspect cat does something similar to what the deprecated stringWithContentsOfFile: did.