如何在 Swift 的 macOS 项目中获取当前打开文档的文件路径?

How to get file paths for current open documents in a macOS project in Swift?

我在 Swift 中有一个 macOS 项目,我想在其中获取当前打开文档的文件路径。如何实现?

编辑:我指的是应用程序本身的打开文档。通常表示为 windows 或制表符。

PS 我检查了这些问题但没有帮助: How to get file path? Loading documents in Document based app Cocoa question: How to get file path for current open document for the front most application

您可以从 NSDocumentController 和每个文档的 URL 中获取文档。

for document in NSDocumentController.shared().documents {
    print("\(document.fileURL)")
}