A search regarding the problem quickly led me to http://developer.apple.com/library/mac/#technotes/tn2002/tn2065.html , which made it easy to come up with a fix. Choose Settings / Environment ... from the menu, then edit the text box labeled "Terminal to launch console programs" to contain the following:
osascript -e 'tell app "Terminal"' -e 'activate' -e 'do script quoted form of "$SCRIPT"' -e 'end tell'
All I did was add the quoted form of in the -e 'do script "$SCRIPT"' part. I hope this will make its way into the next update!
I tested this on my Mac. For some reason there is an extra space at the end of the filename being executed. If I take out the last space (manually from the terminal window seeing the last command ran) then it works. Anything look wrong with this line? I copy and pasted it from the forum.
osascript -e 'tell app "Terminal"' -e 'activate' -e 'do script quoted form of "$SCRIPT"' -e 'end tell'
Thanks,
Lawrence
If you want the Terminal to close when the program finishes, you can modify the command to read as follows:
osascript -e 'tell app "Terminal"' -e 'activate' -e 'do script (quoted form of "$SCRIPT") & "; exit"' -e 'end tell'
This seems slightly less confusing to students, some of whom will otherwise end up with a BUNCH of Terminal windows open, and sometimes interact with an old instance of the program.
You also have to make sure you go into Terminal / Preferences in the menu, select the Settings icon, Shell tab, and under "When the shell exits:", pick "Close if the shell exited cleanly". This also allows you to change the semicolon (;) to the AND operator (&&) and control whether the window closes via the return value from main (return 0; -> Terminal closes, while any non-zero return value keeps the Terminal window open).
Of course, this is slightly inconsistent--it's not the same as the behavior under Windows (and I haven't tested it under Linux, so I don't know how it differs). Since cb_console_runner doesn't seem to exist in the Mac OS X version of C::B, here's an alternative:
osascript -e 'tell app "Terminal"' -e 'activate' -e 'do script (quoted form of "$SCRIPT") & "; echo \'Press any key to continue.\'; read -n 1; exit"' -e 'end tell'
It shows that big "ugly" command in the Terminal window, but at least the behavior is consistent with C::B's behavior under Windows (and what students seem to expect nowadays ... suppressing a rant about "kids" who learn to program only using IDEs and GUIs ... there, I'm all better now). ;-)
My students and I experienced the same problem, so I initially tried something very similar to what was proposed by ProfessorO above:
osascript -e 'tell app "Terminal"' -e 'activate' -e 'do script quoted form of "$SCRIPT"' -e 'end tell'
All I did was add the quoted form of in the -e 'do script "$SCRIPT"' part. I hope this will make its way into the next update!
But, as goetz reported, a trailing space in the contents of the $SCRIPT variable made this not work for me.
To get Code::Blocks to allow spaces in the names of files and folders, I had to use bash string manipulation. Also, because I noticed that the working directory is defaulting to the user's home directory when using the "do script" command (not desirable), I added a command to cd to the project's root directory. Finally, because seeing the entire command appear twice at the top of the terminal window is pretty messy, it also scrolls the text out of view using clear. This makes it so the output of the program is the only thing seen (unless the user scrolls up). The resulting command looks like this:
osascript -e 'tell app "Terminal"' -e 'activate' -e 'do script "exe=\'$SCRIPT\'; cd \\"${exe%/*/*/*}\\"; clear; \\"${exe% }\\""' -e 'end tell'
My students have reported success using it so far, so give it a try if you're having difficulty. =)
Also, I like ProfessorO's idea to have the Terminal window emulate the Windows behavior of closing when the program finishes (as soon as the user presses a key). With this behavior added, the command I posted above becomes this:
osascript -e 'tell app "Terminal"' -e 'activate' -e 'do script "exe=\'$SCRIPT\'; cd \\"${exe%/*/*/*}\\"; clear; \\"${exe% }\\"; echo \'Press any key to continue.\'; read -n 1; exit"' -e 'end tell'
Yes, it's ugly, but I like that it makes for a fairly consistent experience for students on different platforms. =)
Kudos to ProfessorO for the idea. Make sure you follow his directions on how to set the window's behavior to close on exit:
You also have to make sure you go into Terminal / Preferences in the menu, select the Settings icon, Shell tab, and under "When the shell exits:", pick "Close if the shell exited cleanly".
If you also want Code::Blocks to be made the active application when your console application exits (normally, Terminal will remain as the active program and you have to manually return to Code::Blocks), you can use this launcher script:
osascript -e 'tell app "Terminal"' -e 'activate' -e 'do script "exe=\'$SCRIPT\'; cd \\"${exe%/*/*/*}\\"; clear; \\"${exe% }\\"; echo -e \'Press any key to continue...\'; read -n 1; osascript -e \'tell app \\"CodeBlocks\\" to activate\'; exit"' -e 'end tell'