I'm trying to use Pandoc to convert LaTeX to Markdown. I need to create a file and then run the pandoc terminal command. The problem is the file I create isn't in the same directory that I'm running the terminal commands in.
I tried using shell("cd") but it doesn't move you to the user's folder.
Any ideas?
import Cocoa
class ViewController: NSViewController {
func shell(args: String...) -> Int32 {
let task = NSTask()
task.launchPath = "/usr/bin/env"
task.arguments = args
task.launch()
task.waitUntilExit()
return task.terminationStatus
}
override func viewDidLoad() {
super.viewDidLoad()
shell("pwd")
let file = "input.txt"
let text = "\\emph{test}"
if let dir : NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
let inputPath = dir.stringByAppendingPathComponent(file)
//writing
do {
try text.writeToFile(inputPath, atomically: false, encoding: NSUTF8StringEncoding)
shell("pandoc","-f","latex","-t","markdown","input.txt","-o","output.txt")
}
catch {/* error handling here */}
let outputPath = dir.stringByAppendingPathComponent("output.txt")
//reading
do {
let inputText = try NSString(contentsOfFile: inputPath, encoding: NSUTF8StringEncoding)
print(inputText)
let convertedText = try NSString(contentsOfFile: outputPath, encoding: NSUTF8StringEncoding)
print(convertedText)
}
catch {/* error handling here */}
}
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
}
Here is the output
/Users/james/Library/Developer/Xcode/DerivedData/FlashCardPreview-gqzwutewnxspazcdloxqruaikvel/Build/Products/Debug
env: pandoc: No such file or directory
\emph{test}
This is partly because the task's executable is set to env
which itself executes pandoc
; but in the meantime it loses the working directory.
The solution to this is to set launchPath
to the Pandoc executable.
This is also because we have to use inputPath
and outputPath
in the task arguments instead of just the filenames, or to set a currentDirectoryPath
for the task.
Working version 1:
func shell(args: String...) -> Int32 {
let task = NSTask()
task.launchPath = "/usr/local/bin/pandoc"
task.arguments = args
task.launch()
task.waitUntilExit()
return task.terminationStatus
}
and
let file = "input.txt"
let text = "\\emph{test}"
if let dir : NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
let inputPath = dir.stringByAppendingPathComponent(file)
let outputPath = dir.stringByAppendingPathComponent("output.txt")
//writing
do {
try text.writeToFile(inputPath, atomically: false, encoding: NSUTF8StringEncoding)
shell("-f","latex","-t","markdown", inputPath, "-o", outputPath)
}
catch { print(error) }
//reading
do {
let inputText = try NSString(contentsOfFile: inputPath, encoding: NSUTF8StringEncoding)
print(inputText)
let convertedText = try NSString(contentsOfFile: outputPath, encoding: NSUTF8StringEncoding)
print(convertedText)
}
catch { print(error) }
}
Working version 2:
func shell(args: String...) -> Int32 {
let task = NSTask()
task.launchPath = "/usr/local/bin/pandoc"
if let dir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
task.currentDirectoryPath = dir
}
task.arguments = args
task.launch()
task.waitUntilExit()
return task.terminationStatus
}
and
let file = "input.txt"
let text = "\\emph{test}"
if let dir : NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
let inputPath = dir.stringByAppendingPathComponent(file)
let outputPath = dir.stringByAppendingPathComponent("output.txt")
//writing
do {
try text.writeToFile(inputPath, atomically: false, encoding: NSUTF8StringEncoding)
shell("-f","latex","-t","markdown","input.txt","-o","output.txt")
}
catch { print(error) }
//reading
do {
let inputText = try NSString(contentsOfFile: inputPath, encoding: NSUTF8StringEncoding)
print(inputText)
let convertedText = try NSString(contentsOfFile: outputPath, encoding: NSUTF8StringEncoding)
print(convertedText)
}
catch { print(error) }
}
Prints:
\emph{test}
*test*
来源:https://stackoverflow.com/questions/34909890/using-pandoc-with-swift