Posts for Tag: command

"Call piped shell commands from Objective-C"

If you happen to want to know how to run - say - the following command from inside an Objective-C application:

echo "something something" | multimarkdown

So that the fantastic [MultiMarkdown]( runs its magic and transforms **something something** into **\something something\**, then you need to do as below:

- (NSString*) convertMD:(NSString *)data {

// Task for echo NSTask *echoTask = [[NSTask alloc] init]; [echoTask setLaunchPath: @"/bin/echo"]; // Tell which command we are running [echoTask setArguments: [NSArray arrayWithObjects:data, nil]];

// Task for multimarkdown NSTask *mdTask = [[NSTask alloc] init]; // Make a new task [mdTask setLaunchPath: @"/usr/local/bin/multimarkdown"]; // Tell which command we are running [mdTask setArguments: [NSArray arrayWithObjects:@"--snippet", nil]];

// Pipes for input and output NSPipe *pipeBetween = [NSPipe pipe]; // Handles getting the result of echo and injecting into md NSPipe *outPipe = [NSPipe pipe]; // Handles getting the result of md

// Rig it together [echoTask setStandardOutput:pipeBetween]; [mdTask setStandardInput:pipeBetween]; [mdTask setStandardOutput:outPipe];

// Execute [echoTask launch]; [mdTask launch];

// And get the result data NSData *result = [[outPipe fileHandleForReading] readDataToEndOfFile];

return [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding]; }

A couple of notes here:

- What this does is pipe the result of the echoTask (which will run **echo "\"**) as input (standard) to the mdTask, which will run **multimarkdown --snippet**; - The outPipe will simply take the output of the mdTask so we can return what multimarkdown created; - The commands need the full path in order for the tasks to run; - In case you are curious, i'm creating a markdown editor for OSX with realtime preview inline; - If you want to test this particular piece of code as-is, make sure you [install multimarkdown for OSX first](


FISH Shell - How to set tmux window/pane title based on current command and directory

Here's a neat trick: Setting the current tmux window/pane title to the current running command. In case there is no command running, show the current working directory. But trim it a little, so we don't end up with huge window titles :)

In your file add the following functions:

# Set the tmux window title, depending on whether we are running something, or just prompting function fish_title if [ "fish" != $_ ] tmux rename-window "$_ $argv" else tmux_directory_title end end

function tmux_directory_title if [ "$PWD" != "$LPWD" ] set LPWD "$PWD" set INPUT $PWD set SUBSTRING (eval echo $INPUT| awk '{ print substr( $0, length($0) - 19, length($0) ) }') tmux rename-window "..$SUBSTRING" end end

The first one is a special function called by fish on every command execution. I'm using it to see what is being ran ($_ gives you the command in execution). If it's fish itself, then call tmux\_directory\_title to set PWD as the title.

The second function - as explained - sets the title as grabbed from the current working directory (PWD). Except it performs a little AWKing to trim it down to 20 chars max (the last ones).