*24 Jan 2015*
<div class="posthaven-post-body">
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\](http://fletcherpenney.net/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\](http://fletcherpenney.net/multimarkdown/download/).
Peace
</div>
[[tags/cocoa|cocoa]] [[tags/command|command]] [[tags/objectivec|objectivec]] [[tags/osx|osx]] [[tags/pipe|pipe]] [[tags/shell|shell]] [[tags/task|task]]