MacOS swift:从 Pipe() 获取输出数据时遇到问题
我正在尝试运行命令行来在 Xcode 项目中截取屏幕截图并使用 Pipe() 来记录输出数据。 当我在 Xcode 中构建并运行这个项目时,我可以获得字符串形式的输出信息,就像这个屏幕截图的基本信息一样。但是当我将该项目归档为 MacOS 应用程序并运行它时,出现错误。我无法获取输出数据的任何信息。
“outpipe.fileHandleForReading.readDataToEndOfFile()”的结果为空。
这是我的代码:
let task = Process()
task.launchPath = "/usr/sbin/screencapture"
var arguments = [String]();
arguments.append("-s")
let ScreenshotPath = "screenshotpath.jpg"
arguments.append(ScreenshotPath)
task.arguments = arguments
let outpipe = Pipe()
task.standardOutput = outpipe
task.standardError = outpipe
do {
try task.run()
} catch {
print("error in process")
}
let outputData = outpipe.fileHandleForReading.readDataToEndOfFile()
let resultInformation = String(data: outputData, encoding: .utf8)
task.waitUntilExit()
print(resultInformation)
有人可以帮我解决这个问题吗?太感谢了!
I am trying to run command lines to take screenshots in my Xcode project and using Pipe() to log output data.
When I build and run this project in Xcode, I can get the output information as a string, like this screenshot's basic information. But when I archive this project as a MacOS applicaiton and run it, I get an error. I can't get any information for outputData.
The result of "outpipe.fileHandleForReading.readDataToEndOfFile()" is empty.
Here are my codes:
let task = Process()
task.launchPath = "/usr/sbin/screencapture"
var arguments = [String]();
arguments.append("-s")
let ScreenshotPath = "screenshotpath.jpg"
arguments.append(ScreenshotPath)
task.arguments = arguments
let outpipe = Pipe()
task.standardOutput = outpipe
task.standardError = outpipe
do {
try task.run()
} catch {
print("error in process")
}
let outputData = outpipe.fileHandleForReading.readDataToEndOfFile()
let resultInformation = String(data: outputData, encoding: .utf8)
task.waitUntilExit()
print(resultInformation)
Can somebody help me with this out? Thank you so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
主要问题是您试图在捕获任何输出并将其写入磁盘之前从文件中读取数据。
“-s”选项指定鼠标选择模式,该模式是交互式的。尝试从命令行运行它:
screenshot -s snapshot.jpg
您最终应该会看到一些带有 x 和 y 坐标的大十字线。
在这种情况下,在完成选择之前不会写入输出文件。
另一个问题是您试图读取文件的内容而不知道它是否真正完整。您可以使用完成块的终止处理程序,如下所示:
这将确保您在实际写入文件之后读取文件。希望有帮助。
The main issue is that you are trying to read from your file before any output is captured and written to disk.
The '-s' option specifies mouse selection mode, which is interactive. Trying running it from the command line as:
screenshot -s screenshot.jpg
You should end up seeing some large crosshairs with x and y coordinates.
The output file isn't written in this case until you finish your selection.
The other problem is that you are trying to read the contents of the file without knowing if it is actually complete. You could use the terminationHandler of the completion block as in:
This will ensure you read the file after it's actually written. Hope that helps.
我认为问题在于将 Pipe 对象分配给
task.standardOutput
和task.standardError
。我建议创建两个不同的 Pipe 对象,然后分别将它们传递到输出。我们在 Mac App Store 的生产中使用了此答案中的函数。
I believe the issue is assigning your Pipe object to both
task.standardOutput
andtask.standardError
. I recommend creating two distinct Pipe objects, then passing them to the outputs respectively.We used the function from this answer in production on the Mac App Store.