How to view stdout of script run within automator
Asked Answered
M

2

5

I have a python script I want to be able to drag-drop files on to. So I've wrapped it in an Automator Application. Said Application has a single Run Shell Script with contents that look something like:

export PATH=${PATH}:/usr/local/bin:/usr/local/CrossPack-AVR/bin
cd /Applications/MyApp
/Applications/MyApp/doIt.py "$1"

This works. My python script runs with $1 showing up in its sys.argv[1]. Said script produces quite a bit of output though. I'd like to open some sort of window that shows the output as it happens. I don't mind if the user has to close it. I don't see anything in Automator's actions that do that. I've tried to do something like:

open -a Terminal /Applications/MyApp/doIt.py "$1"

Unfortunately, that doesn't seem to inherit the environment. Nor does it hand the $1 to the python script, but tries to open it as well.

So I'm looking for a way, to open something (Terminal or whatever) that can capture and scroll the stdout of that script while it runs, as invoked from within the script.

Moses answered 20/3, 2014 at 0:18 Comment(0)
P
4

I am not at my Mac, so this is untested but I am pretty sure it can be made to work with minor edits...

Change your script so it looks like this and your python script runs in the background saving its output to a temporary file $$.tmp where $$ is your process id (pid):

export PATH=${PATH}:/usr/local/bin:/usr/local/CrossPack-AVR/bin
cd /Applications/MyApp
/Applications/MyApp/doIt.py "$1" > $$.tmp &

Now add the following lines at the end, so that you 1) create a script that tails your log file, 2) make it executable and 3) you execute it:

echo "tail -f $$.tmp" > x.command
chmod +x x.command
open x.command

I would recommend renaming x.command as $$.command so that you can run it multiple times from multiple users without interactions between the various users. You should also clean up and delete the temporary files after use.

Paulettapaulette answered 21/3, 2014 at 14:40 Comment(1)
Apparently using $$ to name tmp files is frowned upon as it can generate security holes. TL;DR—Use mktemp. https://mcmap.net/q/150121/-what-does-mean-in-the-shellBayly
J
6

In Automator arguments and variables can be passed using a special variable:

   $@

To get output from a script or task running in Automator you can click on the results (recessed button) below the script to see any output. Additionally you could setup another bash script to pass the output to stdout or wherever else you choose.

This example shows a python script sending output and variables to a bash script. You can pass input as arguments, or to stdin:

automator

Jubbah answered 20/3, 2014 at 1:6 Comment(2)
Doesn't that show the output in automator? Is there not a tool to show stdout as it is produced (not all at once at the end) outside of automator (which is basically an IDE)?Moses
Correct, the (end results) are output in Automator. From my understanding of how Automator works there isn't a straight forward way of getting the stdout to display as a stream - since Automator uses more of a start to finish approach as it's running. There are things you can do such as send stdout to a file and monitor output there, or select "show this action when automator runs" to pause it between tasks. If you really want to see output as you would in terminal then you might want to consider taking a different route instead of Automator perhaps.Ladybird
P
4

I am not at my Mac, so this is untested but I am pretty sure it can be made to work with minor edits...

Change your script so it looks like this and your python script runs in the background saving its output to a temporary file $$.tmp where $$ is your process id (pid):

export PATH=${PATH}:/usr/local/bin:/usr/local/CrossPack-AVR/bin
cd /Applications/MyApp
/Applications/MyApp/doIt.py "$1" > $$.tmp &

Now add the following lines at the end, so that you 1) create a script that tails your log file, 2) make it executable and 3) you execute it:

echo "tail -f $$.tmp" > x.command
chmod +x x.command
open x.command

I would recommend renaming x.command as $$.command so that you can run it multiple times from multiple users without interactions between the various users. You should also clean up and delete the temporary files after use.

Paulettapaulette answered 21/3, 2014 at 14:40 Comment(1)
Apparently using $$ to name tmp files is frowned upon as it can generate security holes. TL;DR—Use mktemp. https://mcmap.net/q/150121/-what-does-mean-in-the-shellBayly

© 2022 - 2024 — McMap. All rights reserved.