2

Problem:
I'm trying to hook up the standard input/standard output from a unix executable file to the user interface in a MacOS application. But, I can't seem to access the values - nothing shows up.

Background:
I've implemented every solution that I could find, but none of them have worked [1][2][3][4][5][6][7][8][9]. I've completed a Python 3 course [1], so that I could customize the standard output in the python script of the executable file [1]. And, I've reviewed and implemented several working MacOS repositories that update their user interface with data from standard output [1][2][3].

Code: Full

func runExecutable() {

    let desktop = fileManager.urls(for: .desktopDirectory, in: .userDomainMask)[0]
    let url = Bundle.main.url(forResource: "youtube_dl_custom", withExtension: "")
    let arguments = [
        "--format",
        "bestvideo[ext=mp4]+bestaudio[ext=m4a]",
        "--output",
        "\(desktop)/%(title)s.%(ext)s",
        "\(videoUrlTextField.stringValue)"
    ]
    process.arguments = arguments
    process.executableURL = url
    process.standardInput = inputPipe
    process.standardOutput = outputPipe

    openConsolePipe()

    inputPipe.fileHandleForReading.readabilityHandler = {
        [weak self] fileHandle in

        let data = fileHandle.availableData
        self?.buffer.append(data)
        if let buffer = self?.buffer,
            let string = String(data: buffer, encoding: .utf8),
            string.last?.isNewline == true {

            self?.buffer.removeAll()
            print("## \(string)")
            self?.standardOutputTextView.string += string + "\n"

                self?.outputPipe.fileHandleForWriting.write(data)
            }
        }
    try? process.run()
    closeConsolePipe()
}
func openConsolePipe() {
    dup2(STDOUT_FILENO, outputPipe.fileHandleForWriting.fileDescriptor)
    dup2(inputPipe.fileHandleForWriting.fileDescriptor, STDOUT_FILENO)
    dup2(inputPipe.fileHandleForWriting.fileDescriptor, STDERR_FILENO)
}
func closeConsolePipe() {
    freopen("/dev/stdout", "a", stdout)
}

Results:
The standard output appears to automatically print to the console, but I can't seem to access the values.

Misc:
Used the youtube-dlrepository to download videos [1].
Used a custom python script for youtube-dl [1].
Converted youtube-dl to an executable file using pyinstaller [1].
Posted the project to GitHub for troubleshooting [1].

10
  • What do you mean by "linux" here? Do you mean macOS shell? Commented Aug 10, 2019 at 18:03
  • Sorry, I meant unix. Commented Aug 10, 2019 at 18:23
  • 1
    If you want to capture the output from the process then you should attach a readabilityHandler to the outputPipe, not to the inputPipe. – Possibly helpful: stackoverflow.com/q/29548811/1187415. Commented Aug 10, 2019 at 20:21
  • Re: "but I can't seem to access the values", which values are what are you doing with them? Commented Aug 10, 2019 at 20:27
  • @Martin, thanks for the suggestion! I will try again with the output pipe. I forgot to mention, this code is only the most recent attempt. I've literally tried the code from nine different solutions, including the one in your suggestion. But, I'll try that one again! And, you've helped me realize I should've posted the titles instead of just the links. So, thank you! Commented Aug 10, 2019 at 23:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.