Swift NSTask没有执行一些终端命令

时间:2018-02-28 19:31:34

标签: swift terminal nstask

我正在使用Swift游乐场试验执行终端命令来启动应用程序。在下面的代码中,task,task1和task2工作,所以我知道pdflatex(我的应用程序)及其目标(test.tex)存在且可访问。使用task3,我尝试按照task,task1和task2的模式在目标上执行我自己的应用程序。我收到以下错误:

"错误域= NSCocoaErrorDomain代码= 4"文件“pdflatex”不存在。" UserInfo = {NSFilePath = / / usr / local / texLive / 2015 / bin / x86_64-darwin / pdflatex} \ n"

这似乎与Task1的结果相矛盾。

在task4和task5中,我只是尝试模仿成功启动进程的终端命令。这些给我错误信息:

"错误域= NSCocoaErrorDomain代码= 4"文件“cd”不存在。" UserInfo = {NSFilePath = / cd} \ n"

错误域= NSCocoaErrorDomain代码= 4"文件“pdflatex”不存在。" UserInfo = {NSFilePath = / pdflatex} \ n"

(我也尝试用" / usr / bin / cd"替换" cd"命令,但这没有区别。)

为了它的价值,我使用的是tcsh。我的unix有点生疏,我处于Swift NSTask学习曲线的最底层。

代码遵循:

//:游乐场 - 名词:人们可以玩的地方

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")

0 个答案:

没有答案