How to override the default window opening behavior for a subprocess?

Viewed 20

I would like to run a process from swift. That process opens a window, and I would like to prevent that from happening.

Here is my code as of now:

//
//  main.swift
//

import Foundation
import Cocoa

let programProcess = Process()

programProcess.executableURL = URL(fileURLWithPath: "./program")

do {
    let programProcess = try programProcess.run()
}
catch {
    print("error \(error)")
}

while (true) {
    let options = CGWindowListOption(arrayLiteral: CGWindowListOption.excludeDesktopElements, CGWindowListOption.optionAll)
    let windowListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0))
    let windowInfoList = windowListInfo as NSArray? as? [[String: AnyObject]]
    let programWindow = windowInfoList?.filter{ $0["kCGWindowOwnerPID"] as! Int32 == programProcess.processIdentifier }
    
    if (programWindow?.isEmpty == false) {
        print(programProcess.processIdentifier)
        print(programWindow)
        let programApp = NSRunningApplication(processIdentifier: programProcess.processIdentifier)
        programApp?.hide()

        break
    }
    
}

The code runs the program and hides the window as soon as it opens. Can I do better than this? Like, having the app not opening a window at all?

0 Answers
Related