8

I'm making a console-based application in Objective-C which relies on being able to clear the console periodically. How can this be done? All I've seen on SO and Google were ways to have the developer clear the console with X-Code, but that will not do.

One solution I found on Yahoo! Answers told me to do the following, but it does not run due to being unable to find a file:

NSTask *task;
task = [[NSTask alloc]init];
[task setLaunchPath: @"/bin/bash"];

NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"clear", nil];
[task setArguments: arguments];

[task launch];
[task waitUntilExit];
Ky -
  • 30,724
  • 51
  • 192
  • 308

4 Answers4

6

Try using :

system( "clear" );

Important headers :

#include <stdlib.h>

Hint : Objective-C is still C, right?


UPDATE :


In case of a "TERM environment variable not set." error :

1) Run the program, directly from your terminal (or just ignore the error while testing it in Xcode; it's supposed to run in a normal terminal anyway, huh?)

2) Set the TERM variable in your Scheme's settings. To what? Just run this in your terminal to see what "TERM" should be :

DrKameleons-MacBook-Pro:Documents drkameleon$ echo $TERM
xterm-256color

enter image description here

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • Objective-C is still C, but I prefer "native-feeling" solutions :) – houbysoft Apr 10 '12 at 03:19
  • 1
    @houbysoft I do too; definitely. But honestly isn't using `NSTask` too much for something as simple as an `system("clear")`? The Cocoa way may at times be unnecessarily verbose... :-) – Dr.Kameleon Apr 10 '12 at 03:22
  • All this does is print "TERM environment variable not set." and continue with the program – Ky - Apr 10 '12 at 03:51
  • @Supuhstar *"The error message you're seeing says that the TERM environment variable isn't set, probably because you're trying to launch your program from within Xcode."* -- http://stackoverflow.com/questions/8470184/what-does-term-environment-variable-not-set-mean – Dr.Kameleon Apr 10 '12 at 03:56
  • Thanks for the diagnosis, but what's the fix? – Ky - Apr 10 '12 at 04:01
  • Seems descriptive and helpful, but I've scoured Xcode's preferences four times over and didn't see so much as a mention of a scheme, much less its settings. – Ky - Apr 10 '12 at 04:22
  • 2
    In the "Executables" group, select the executable for your program and Get Info. In there, there's a place to set environment variables when running from Xcode. – Ken Thomases Apr 10 '12 at 05:04
  • I've added your code to mine, added a TERM variable to Xcode's project settings with the same value given by `echo $TERM` in the Terminal (in my case, `xterm-color`), and ran it. It still tells me `TERM environment variable not set` – Ky - Apr 10 '12 at 07:47
  • Then you must have done it incorrectly, I'm afraid. I just tested with Xcode 3.2.6, with the code from my answer. In my case, it doesn't clear Xcode's console window, because that's not a real terminal, but it printed the appropriate escape sequence and didn't error out. You're not going to find any solution which clears the Xcode console window, I don't think, because it would require Xcode to handle that and I doubt Xcode is coded to. In a Terminal window, my test worked completely. – Ken Thomases Apr 11 '12 at 05:32
4

The way to do this without spawning a subprocess is to use ncurses.

#include <curses.h>
#include <term.h>
#include <unistd.h>

int main(void)
{
    setupterm(NULL, STDOUT_FILENO, NULL);
    tputs(clear_screen, lines ? lines : 1, putchar);
}

Compile with -lncurses.

The setupterm() call only needs to be done once. After that, use the tputs() call to clear the screen.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • Looks promising, but I can't figure out how to add command-line options for compiling with XCode – Ky - Apr 10 '12 at 03:57
  • Add "-lncurses" to the Other Linker Flags (OTHER_LDFLAGS) build setting for the target. – Ken Thomases Apr 10 '12 at 04:01
  • `TERM environment variable not set. Debugger stopped. Program exited with status value:1` – Ky - Apr 10 '12 at 04:26
  • @Supuhstar: set an environment variable `TERM` in the environment section of the Arguments tab in the scheme for running your application. The Apple terminal app uses `xterm-256color` as its value – JeremyP Apr 10 '12 at 13:36
3

Why /bin/bash?

Just do:

NSTask *task = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/clear" arguments:[NSArray array]];

Alternatively, using the C way:

#include <stdlib.h>

...
system("/usr/bin/clear");
...
houbysoft
  • 32,532
  • 24
  • 103
  • 156
  • The first one does nothing at all, and the second one just prints "TERM environment variable not set." and continue with the program. – Ky - Apr 10 '12 at 03:54
1

You can use apple script

tell application "Console"
    activate
    tell application "System Events"
        keystroke "k" using command down
    end tell
end tell  

Use NSAppleScript class for executing applescript from obj-C program.

NSAppleScript *lClearDisplay = [[NSAppleScript alloc] initWithSource:@"tell application \"Console\"\n \
                                activate\n \
                                tell application \"System Events\"\n \
                                keystroke \"k\" using command down\n \
                                end tell\n \
                                    end tell "];
NSDictionary *errorInfo;
[lClearDisplay executeAndReturnError:&errorInfo];

NOTE:
If Apple changes or removes ⌘k as the key command for clear display, that will break script.

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144