Please tell me why licenceUrl is nil???

I want to open a text file where all the Licence stuff is written down, I want to do it like this in a macOS app, but the app Bundle does not work for some reason:

import SwiftUI

@main
struct menuHelpApp: App {  

    func androidTools(tool: URL, arguments: [String],  completionHandler: @escaping (Int32, Data) -> Void) throws {

        let group = DispatchGroup()

        let pipe = Pipe()

        var standardOutData = Data()



        group.enter()

        let proc = Process()

        proc.executableURL = tool

        proc.arguments = arguments

        proc.standardOutput = pipe.fileHandleForWriting

        proc.terminationHandler = { _ in

            proc.terminationHandler = nil

            group.leave()

        }
        group.enter()

        DispatchQueue.global().async {

            let data = pipe.fileHandleForReading.readDataToEndOfFile()

            pipe.fileHandleForReading.closeFile()

            DispatchQueue.main.async {

                standardOutData = data

                group.leave()
            }
        }
        group.notify(queue: .main) {

            completionHandler(proc.terminationStatus, standardOutData)

        }

        try proc.run()

        pipe.fileHandleForWriting.closeFile()
    }

 
    var body: some Scene {

        WindowGroup {

            ContentView()

        }

        .commands {

            CommandGroup(replacing: .help) {
                let licenceUrl = Bundle.main.url(forResource: "textFiles/Licence", withExtension: "txt")

                Button(action: {
                    try! androidTools(tool: URL(fileURLWithPath: "/usr/bin/open", relativeTo: nil), arguments: ["-a", "TextEdit", licenceUrl!.absoluteString]) { (status, outputData) in
                        let output = String(data: outputData, encoding: .utf8) ?? ""
                        print(output)
                    }
                }) {
                    Text("AppSignerGui License")
                }
            }
        }
    }
}
Answered by OOPer in 684105022

I guess you have not added the folder textFiles successfully.

If the folder is added to the project, it is shown in a bluish color, not dark-yellowish.

Can you explain what the app Bundle does not work more precisely? What do you expect with your code? What actually happens with your code?

In my Project I have a .txt file under textFiles/Licence.txt where the Licences are displayed. At licenceUrl.absoluteString I get the error "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"

Sorry, but you have not answered to What do you expect with your code?, so the problem is not clear enough. Anyway, I will write an answer based on my guess.

I expect to open a text file with the default Texteditor when I click on Help > Button AppSignerGui License. Quote from my Question: "I want to open a text file where all the Licence stuff is written down"

Accepted Answer

I guess you have not added the folder textFiles successfully.

If the folder is added to the project, it is shown in a bluish color, not dark-yellowish.

Please tell me why licenceUrl is nil???
 
 
Q