How to start up another process in a new Terminal window

Hi,

I have a program that runs on the command line in Terminal. Within the program, I need to be able to start up a new Terminal window and start a new instance of the program (with different arguments). I am able to accomplish this using xterm. But instead of relying on xterm, I would like to accomplish this in Terminal. The program produces useful screen output so this is the reason a new Terminal window needs to be opened.

Does anyone have a suggestion how I can accomplish this? I have looked extensively into the "open" command with no solution. The open command will start up a new instance of the program, but it does not process the arguments supplied to the program. Even using the --args argument for open, my Fortran program completely ignores these arguments.

Thank you for any suggestions.

Mac Pro, Mac OS X (10.6.7)

Posted on Mar 30, 2011 1:21 PM

Reply
16 replies

Mar 30, 2011 3:36 PM in response to Tony T1

There must be a better way, but unless you get a better response:


#!/bin/bash

# Change" ls" and "ls -l" to your program
ls
osascript -e 'tell application "System Events" to tell process "Terminal" to keystroke "n" using command down'
-e 'tell application "System Events" to tell process "Terminal" to keystroke "ls -l" '
-e 'tell application "System Events" to tell process "Terminal" to key code 36 '

Mar 30, 2011 4:54 PM in response to Tony T1

Thanks for these suggestions. I tried calling a script command as suggested, but ran into a couple of obstacles.

In a single run of the program, I may need to open several Terminal windows all with different arguments passed to the program. In this case, the script command needs to be changeable. I had considered creating a new script file for every instance of the program call, filling in the arguments as needed, for example

#!/bin/bash
# script with arg a
osascript <<EOF
tell application "Terminal"
do script "runpgm arga"
end tell
EOF


#!/bin/bash
# script with arg b
osascript <<EOF
tell application "Terminal"
do script "runpgm argb"
end tell
EOF

#!/bin/bash
# script with arg c
osascript <<EOF
tell application "Terminal"
do script "runpgm argc"
end tell
EOF

and calling each script using the open command.

But the next problem is that the script file supplied to 'open' must have execute permission. Otherwise, user interaction is required to allow the file to be executed. This is not a desirable effect. I need this to run without prompting the user once the program has started. As far as I know, there is no way to create a new file with execute permission within Fortran.

Is there a way to supply an existing script file with arguments from the open command such that the arguments are then passed to my program? For example

#!/bin/bash
# script with arg
osascript <<EOF
tell application "Terminal"
do script "runpgm arg"
end tell
EOF


where I can supply 'arg' on the fly? For example

open runpgm_script --args arga

open runpgm_script --args argb

open runpgm_script --args argc

My attempt was halted because I could not get the arguments to get passed correctly into the script.

Unfortunately, another problem with this is that it assumes the users of this program will be using Mac OS X version 10.6.1 or above where open has the --args option.

Mar 30, 2011 5:36 PM in response to nntwoee

What is the nature of this "runpgm" program? It is a script or something else? You could just change the permissions with "chmod". Or you could run it with its shell using "/bin/sh runpgm argb". The "open" command is really only appropriate for things that the Aqua user interface understands. You wouldn't want to mix "open" with a shell script.

Mar 30, 2011 5:44 PM in response to etresoft

It is a compiled Fortran program. If I use xterm, I can do the following on the command line:

prompt$ runpgm arg1

Then during this process (within runpgm), I can start up several more xterm windows, each starting up a new process as follows

xterm -e 'runpgm arg1a'
xterm -e 'runpgm arg1b'
xterm -e 'runpgm arg1c'

I'm trying to do the same with Terminal and the open command seemed like the only available command where I can start up the program.

Incidentally, if I start runpgm from the command line in a Terminal program, calling xterm for the secondary processes works without any problems. I am just looking for a way to stay within Terminal and not use xterm at all.

Thanks.

Mar 31, 2011 2:55 PM in response to J D McIninch

J D McIninch wrote:
Yet another possibility:


open -a /Applications/Utilities/Terminal.app /usr/bin/top


It starts up the new Terminal window with the specified program, but how do I pass the arguments to that program?

Also tried:


open -a /Application/Utilities/Terminal.app ls -al


and


open -a /Application/Utilities/Terminal.app 'ls -al'


and still no luck.

Mar 31, 2011 6:04 PM in response to nntwoee

I can't understand what your problem is...
If you create a script like

#!/bin/sh
osascript > /dev/null <<EOF
tell application "Terminal"
do script "$*"
end tell
EOF

and name it, say, "newterm", then you can call it as

newterm runpgm arg1a
newterm runpgm arg1b
or
newterm "runpgm arg1c"

Is this what you want?

I guess "open -a Terminal prog --args arg1 arg2" will pass the arg1 arg2 to Terminal.app, not to the prog.

Apr 1, 2011 11:58 AM in response to Jun T.

Jun T. wrote:
I can't understand what your problem is...
and name it, say, "newterm", then you can call it as

newterm runpgm arg1a
newterm runpgm arg1b
or
newterm "runpgm arg1c"

Is this what you want?


Oh, thank you. I have been continually trying to use the 'open' command which was not passing the arguments to the program correctly. By simply calling this 'newterm' script, it seems to pass the arguments as desired. I am trying this from the command line and it is working. But I will check back when I have tried the call from within the program itself.

One added question regarding the script:


#!/bin/sh
osascript > /dev/null <<EOF
tell application "Terminal"
do script "$*"
end tell
EOF


Is there a way to tell Terminal to close itself once the script is finished? I tried the following:


#!/bin/sh
osascript > /dev/null <<EOF
tell application "Terminal"
do script "$*; exit"
end tell
EOF


With this, the Terminal windows shows a logout, but it doesn't close itself. I know there is a setting in Preferences to close the Terminal, but I won't have any control of other users' settings. I don't want these new windows to stay open.

Thank you.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to start up another process in a new Terminal window

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.