I am using a Swift playground to experiment with executing a terminal command to launch an application. In the code that follows, task,task1 and task2 work, so I know that pdflatex (my application) and its target (test.tex) exist and are accessible. With task3 I try to follow the pattern of task, task1 and task2 to execute my own application on its target. I get the following error:
"Error Domain=NSCocoaErrorDomain Code=4 "The file “pdflatex ” doesn’t exist." UserInfo={NSFilePath=/ /usr/local/texLive/2015/bin/x86_64-darwin/pdflatex }\n"
which seems to contradict the result of Task1.
In task4 and task5 I simply try to mimic the Terminal commands that do launch the process successfully. These give me the error messages:
"Error Domain=NSCocoaErrorDomain Code=4 "The file “cd ” doesn’t exist." UserInfo={NSFilePath=/cd }\n"
and
Error Domain=NSCocoaErrorDomain Code=4 "The file “pdflatex ” doesn’t exist." UserInfo={NSFilePath=/pdflatex }\n"
(I have also tried replacing the "cd" command with "/usr/bin/cd", but that makes no difference.)
For what it's worth, I am using tcsh. My unix is a bit rusty and I am at the bottom of the learning curve for Swift NSTask.
Code Follows:
//: Playground - noun: a place where people can play
import Cocoa
import Foundation
var str = "Hello, playground"
var task = Process()
var task1 = Process()
var task2 = Process()
var task3 = Process()
var task4 = Process()
var task5 = Process()
var pipe = Pipe()
task.standardOutput = pipe
task.executableURL = URL(fileURLWithPath: "/bin/ls")
task.arguments = ["-la","/Users/masong/Desktop/SwiftStuff/test.tex"] //multiple options
try task.run()
task.waitUntilExit()
var handle = pipe.fileHandleForReading
var data = handle.readDataToEndOfFile()
var printing = String (data: data, encoding: String.Encoding.utf8)
print(printing!)
task1.executableURL = URL(fileURLWithPath: "/bin/ls")
task1.arguments = ["-la","/usr/local/texLive/2015/bin/x86_64- darwin/pdflatex"] //multiple options
try task1.run()
task1.waitUntilExit()
handle = pipe.fileHandleForReading
data = handle.readDataToEndOfFile()
printing = String (data: data, encoding: String.Encoding.utf8)
print(printing!)
task2.executableURL = URL(fileURLWithPath: "/bin/cat")
task2.arguments = ["/Users/masong/Desktop/SwiftStuff/test.tex"]
try task2.run()
task2.waitUntilExit()
handle = pipe.fileHandleForReading
data = handle.readDataToEndOfFile()
printing = String (data: data, encoding: String.Encoding.utf8)
print(printing!)
// print(Thread.callStackSymbols)
//task3.executableURL = URL(fileURLWithPath: " /Users/masong/Desktop/SwiftStuff/pdflatex ")
task3.executableURL = URL(fileURLWithPath: " /usr/local/texLive/2015/bin/x86_64-darwin/pdflatex ")
task3.arguments = [" /Users/masong/Desktop/SwiftStuff/test.tex "]
print("Tex in 1")
do {
try task3.run()
}catch{
print("Unexpected error 1")
print(error)
}
task3.waitUntilExit()
print("Tex out 1")
task4.executableURL = URL(fileURLWithPath: "cd ")
task4.arguments = [" /Users/masong/Desktop/SwiftStuff " ]
do {
try task4.run()
}catch{
print(error)
}
task5.executableURL = URL(fileURLWithPath: "pdflatex ")
task5.arguments = [" test.tex "]
print("Tex in 2")
do {
try task5.run()
}catch{
print("Unexpected error 2")
print(error)
}
task5.waitUntilExit()
print("Tex out 2")