If no Terminal app is open, the following code opens TWO Terminal windows. Why is it doing this? I only want one window to open.
If only one Terminal window is open, then the following code opens only ONE additional window.
NSAppleScript* terminal = [[NSAppleScript alloc] initWithSource:
[NSString stringWithFormat:
@"tell application \"Terminal\"\n"
@" activate\n"
@" do script \"echo %@\"\n"
@" tell the front window\n"
@" set title displays shell path to false\n"
@" set title displays custom title to true\n"
@" set custom title to \"My session! %@\"\n"
@" end tell\n"
@"end tell", name, name]];
[terminal executeAndReturnError:nil];
The
do scriptcommand, as you’ve written it, will always run in a new window. If you’d like it to run in a specific window, use the following format:do script (...) in (window...). Terminal’sinsyntax can also handle running scripts in tabs.For instance, if you’d like to run a script in the frontmost window, you can write
do script "echo Hello, world!" in front window.Edit: To follow up, if you’d like to always run the script in a window (create a new one if none are open), you can use the following AppleScript:
Of course, you’ll need to correctly escape it in an
NSArray, but that I’m sure you can do.