1

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")
  • There are various problems here, such as leading or embedded space characters in the file paths. Also note the `cd` is a *shell built-in* and cannot be used to change the working directory for the next process. – Martin R Feb 28 '18 at 19:56
  • @Martin R Thank you very much for your help. Yes, correcting the blank spaces gets me through Task3 and changing the "cd" to "'/usr/bin/cd" then gets me past Task4. My purpose in using "cd" was to try and mimic literally what I would do at the command line. My application writes several output files and I wanted those to appear in the folder where test.tex resides. If "cd" does not work for this purpose, do you know how to direct those product files to the correct destination using NSTask? I have tried some things I found online, but no luck yet. Thank you again! – Black Feather Mar 01 '18 at 23:52
  • 1
    You can set the `currentDirectoryURL` property of the process before you start it. – Martin R Mar 02 '18 at 08:47
  • That does it. Thank you once again! – Black Feather Mar 02 '18 at 17:17

0 Answers0