code to close terminal window?

I am running code in an IDE and want to be able to have any existing terminal window close before the code is run. This way I don't have to keep closing them manually every time I run the code again. I'm coding in C. Any ideas?


Thanks

Posted on Aug 10, 2013 12:21 PM

Reply
13 replies

Aug 10, 2013 6:26 PM in response to Kerose

What about setting the


Terminal -> Preferences -> Settings -> Shell -> When the shell exits: Close the window


Now just make sure that the last thing your script does is exit from the shell and your window will be closed.


Then again, do you "Really" need a Terminal session to run your code?


That is to say, running programs and scripts can be done without needing a terminal session. The Terminal session is just a window to the command line, and you do not need to display anything via the Terminal display, then there is no reason to have it open to run your program.


You can use Applescript ("do shell script" statement), or Automator ("run shell script" action), or you can use a 3rd party utility such as Platypus that will put your program or script in a double clickable app that does not need a Terminal session.


Another option is to use a different Terminal emulator, such as iTerm which also has the ability to close the window when the shell exits. It can even automatically quit iterm when the last window is closed.

Aug 10, 2013 6:35 PM in response to BobHarris

Well, okay, let me give more info...I'm writing a simple program in C using CodeBlocks. when I click RUN or Build and RUn, etc. it opens a terminal window to display the output. For instance, if I'm running a program that finds all the prime numbers from 1-39. I don't want to CLOSE the window AFTER I run it, because then I won't be able to see the results before it closes,,,,I'd like to have the code close the terminal window that is already open because every time I run the code from the IDE (Codeblocks) it opens a new terminal window...


I don't know that I NEED a terminal window to run the code, but I don't know how else I would printout the results...and Codeblocks does this automatically.


Maybe that clarifies things a bit..I couldn't find anything that would apply to settings within CodeBlocks...I don't see why...if there is a terminal window already open, that it would need to open a new one every time. BTW,, I'm quite the amateur here. THanks for your help.

Aug 10, 2013 8:01 PM in response to etresoft

is there an option to do it in Xcode? otherwise that doesn't solve the issue. I was looking for a C coding option, so that wouldn't make a difference which IDE I use. But, if there is a way to set Xcode prefs to achieve that, than I would. But, I normally code in CodeBlocks, because that is what I learned. Would switch though if possible to achieve what I'm after, I know that I had the same issue in there, so unless there is a pref setting, it won't help. Honestly, I might be making too much of a problem out of it than it is worth.


It would have to be something like.."if terminal window is open, close....then run main" or whatever..but I don't know if that is even possible.. ****, I hate programming.....that's why I'm going into power as an EE, staying away from the software side!!!!

Aug 10, 2013 8:12 PM in response to Kerose

Kerose wrote:


is there an option to do it in Xcode? otherwise that doesn't solve the issue. I was looking for a C coding option, so that wouldn't make a difference which IDE I use. But, if there is a way to set Xcode prefs to achieve that, than I would. But, I normally code in CodeBlocks, because that is what I learned. Would switch though if possible to achieve what I'm after, I know that I had the same issue in there, so unless there is a pref setting, it won't help. Honestly, I might be making too much of a problem out of it than it is worth.



Launch Xcode > File > New > Project > Application > Command Line Tool > Next > Type > C


****, I hate programming.....that's why I'm going into power as an EE, staying away from the software side!!!!


I feel the same way about hardware. Don't even bother trying to explain a pull up resistor to me.

Aug 10, 2013 8:42 PM in response to etresoft

haha, !! yeah, it's funny when my professor back in "digital systems" class was always talking about a pull up resistor he looked at the EE guys like "you know whay i'm talking about"!!!


anyways, that is nice. Xcode has it's own output window which works great, but IDK if the extra burden of having to create an Xcode Project for every C file I have is worth it, also, IDK if I'm running something wrong, but it doesn't create an object file or an executable as Codeblocks does.


I'm on the verge of just dealing with closing the terminal window everytime, just hoped I could find a solution. But, that is prolly just a situation I have to deal with using this particular IDE

Aug 10, 2013 8:45 PM in response to Kerose

Electrical Engineering Power. Lets see, imaginary numbers represent power lost due to motor inductance collapsing causing power to flow back towards the generator resulting in additional power loss due to transmission line resistance. So then you figure out where to put capacitor banks to capture that reverse current and shorten the distance the reverse current travels.


Note, EE's are often involved in writing "Load Flow" analysis programs :) then again maybe they have all of them written now, from when I was a co-op student at PP&L :)


You can use AppleScript to tell the Terminal to do things. The cheap way from your C code would be to use a system() call invoking the "osascript -e '…' "command where '…' is the AppleScript you research and provide telling the Terminal to close the window.


Then again, you could also use osascript to tell the Mac to display a dialog box with the information you want to present. I assume you want to do this as a quick and easy instead of actually having you program use Mac OS X GUI API's.

Aug 10, 2013 9:11 PM in response to Kerose

for some reason your post isn't showing up...but i got it in my email feed...anyways, yeah, actually, you seem to understand that more than I do right now!! but everything you said is on point...inductance causes reactive power losses and their is usually a capacitor bank to adjust for that loss in reactive power. I'll be learning much more about that in my senior year


as far as dealing with applescripts...I'd rather not, I think i'm just going to deal with it. I'll mark your post as solving my question as I don't seem to have any other option within the constraints of what I'm doing..Thanks for the help. It's hard enough learning C, let alone trying to mess with scripts and the like!!!

Aug 10, 2013 9:47 PM in response to Kerose

Kerose wrote:


IDK if the extra burden of having to create an Xcode Project for every C file I have is worth it, also, IDK if I'm running something wrong, but it doesn't create an object file or an executable as Codeblocks does.

Xcode does create object files and executables, they are just hard to find.


While an EE may not need to know how to deal with Xcode, you will need to know your way around a UNIX-like operation system. Perhaps instead of using any IDE, you should use vi and possibly a makefile and do everything on the command line. Of course, you can always just open the .c file in Xcode and it will do that perfectly fine. You aren't really using it as an IDE that way, but just as a source code editor. But you can create an makefile-based project, use Xcode as a code editor and IDE, and have all of your code, objects, and output all right in front of you.


I've known many EEs over the years and a fair number of them have would up being programmers. All of them knew their way around UNIX and vi.

Aug 10, 2013 10:32 PM in response to etresoft

yeah, I'm going to be learning how to compile and whatnot from the command line in my next class. Microcontrollers...I've already started working on using makefiles. I don't like it!! haha, but yeah, I assume the knowledge is going to be beneficial.


I would have gone into software engineering or something in computer science, but I knew i never wanted to mess with software...turns out I have to get my hands in there at least a bit! It's just not my strong point. Hoping I never have to get into it in my future, but at least I will have a little background. Either way, I'm hoping the company I work for will just hire a computer guy to do programming!!! But, I try, because I want to have as many tools in my shed as possible.

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.

code to close terminal window?

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