views:

166

answers:

5

I'm debugging a service I'm developing, which basically will open my .app and pass it some data to stdin. But it doesn't seem like it's possible to something like:

open -a myapp.app < foo_in.txt

Is it possible to pass stuff to an .app's stdin at all?

Edit: Sorry, I should have posted this on SO and been more clear. What I'm trying to do is that I have an app made in Python + py2app. I want to be able to handle both when a user drops a file, and use it as a service. The first case isn't a problem since py2app has argv_emulation. I just check if the first argument is a path.

But reading from stdin doesn't work at all, it doesn't read any data regardless if I do as the example above or pipe it. If I pass stdin data to the actual python main script, it works. So I rephrase my question, is it possible to read from stdin with a py2app bundle?

A: 

Well,

open -a /Applications/myapp.app < foo_in.txt

will open foo_in.txt in your myapp.app application. You need the full path of the application, be it Applications, bin, or wherever it is...

It depends on what your application does. This may be more appropriate:

cat foo_in.txt | your_command_goes_here

That will read the contents of foo_in.txt (with cat) and pass them to stdin (with the pipe), so then you just follow that with your command / application.

ridogi
Did you try this? Or do you mean *will open **the contents of** foo_in.txt in your myapp.app application*?
Arjan
The open command will open an file in an application in the GUI. The second example passes the contents to stdin. The same effect could be had with your_command_goes_here < foo_in.txt
ridogi
So, **contents** it is indeed. Still: did you try this? Apparently the questioner tried the same as well (surely the application opened for the question asker, but it somehow did not get the contents of that file passed in).
Arjan
Yes, cat foo_in.txt | your_command_goes_here will definitely pass the contents of the file to the command after the pipe. The command following the pipe has to be able recognize stdin, so perhaps we just need more details from the original poster as to what exactly they are doing.
ridogi
A: 

To start Finder as root, one would not use:

sudo open -a /System/Library/CoreServices/Finder.app

The above runs open as root, but still open runs Finder as the normal user. Instead, one would use:

sudo /System/Library/CoreServices/Finder.app/Contents/MacOS/Finder

So, following that, maybe (I am really just guessing) one needs:

myapp.app/Contents/MacOS/myapp < foo_in.txt
Arjan
myapp.app/Contents/MacOS/myapp is not a unix command, just a path. I think the original poster is trying to pass standard in to their command. If they are trying to pass standard in to a GUI app that will involve Apple Script.
ridogi
You're loosing me. `myapp.app/Contents/MacOS/myapp` in my example is as much a command (an executable) as `Finder.app/Contents/MacOS/Finder` is (as defined as Executable file in the bundle's `info.plist`).
Arjan
My bad, you are correct that it is not just a path, but I don't think it is possible to pass stdin to the executable of a GUI app, although it can be done with Apple Script like I said.
ridogi
A: 

You should almost certainly be doing this through Mach ports or Distributed Objects or pretty much any other method of interapplication communication the OS makes available to you.

Azeem.Butt
+1  A: 

What do you mean with using it as a service?

The example you show won't work, the open command calls LaunchServices to launch the application, and there is no place in the LaunchServices API to pass stdin data or similar to the application.

If you mean adding an item to the OS X Services Menu, you should look at the introductory documentation for developers.

kaizer.se
Thanks, yes I meant the Services menu and this was what I was looking for. I'll have to investigate if developing a service this way is possible with py2app.
pojo
A: 

open creates an entirely new process. Therefore do not use it to redirect stuff into an application from Terminal. You could try

./Foo.app/Contents/MacOS/Foo < Foo.txt

Already mentioned cat Foo.txt | ./Foo.app/Contents/MacOS/Foo very much depending on whether you set Foo as execurtbale and it's in your path. In your case I'd check the .app package for a Ressources folder, that may contain another binary. A *.app Package is a directory. It cannot handle commandline arguments.

wishi