---
title: "Running a script with NSTask and NSPipe"
slug: running-a-script-with-nstask-and-nspipe
section: tech
date: 2016-01-11T20:29:00.000Z
canonical: https://roland.leth.ro/blog/tech/running-a-script-with-nstask-and-nspipe
---

Say you have a Mac app and you want to run a script, either to perform some action, or to return something, here's one way to do it.

First, we create an `NSTask`, set the launch path of the handler, in our case `ruby` (we will use the default one, to be sure it exists), and the required parameters, if required:

```swift
let task = Process()
task.launchPath = "/usr/bin/ruby"
task.arguments = [
  Bundle.main.path(forResource: myScript, ofType: "rb")!,
  parameter1
]
```

This would be the same as running in the terminal:

```bash
ruby /path/to/myScript parameter1
```

All good, but what if the script returns something and we want to use that? `NSPipe` to the rescue:

```swift
let pipe = Pipe()
task.standardOutput = pipe
task.launch()

let resultData = pipe.fileHandleForReading.readDataToEndOfFile()
let resultString = String(data: resultData, encoding: .utf8)
```

Returning a value with a script is as easy as printing:

```ruby
def optional_method
  'I am the return value'
end

puts optional_method + ' of a script'
```

This would result in `resultString`'s value to be *I am the return value of a script*.