[英]How to programmatically open a file from FileProvider Extension (macOS)
Is there a way to programmatically open a file from FileProvider Extension?有没有办法以编程方式从 FileProvider Extension 打开文件?
My use case is that I have FileProviderUI-extension for macOS which shows a dialog with 'Open file' button.我的用例是我有适用于 macOS 的 FileProviderUI 扩展,它显示带有“打开文件”按钮的对话框。 And I'd like when user clicks on 'Open file' button to open the relevant file.
我想当用户点击“打开文件”按钮打开相关文件时。
However, when I execute:但是,当我执行时:
NSWorkspace.shared.open(itemURL)
a Finder error is shown: The application “MyAppAction (Finder)” does not have permission to open “My File.txt.”显示 Finder 错误:应用程序“MyAppAction (Finder)”无权打开“My File.txt”。
I guess this is related to FileProviderExtension being sandboxed, but is there a way to open FileProvider files programmatically?我想这与 FileProviderExtension 被沙盒化有关,但是有没有办法以编程方式打开 FileProvider 文件?
Here is an example on how to open a FileProvider folder:以下是有关如何打开 FileProvider 文件夹的示例:
let fileProviderFolderURL = try! await NSFileProviderManager(for: SREG.context.domain)?.getUserVisibleURL(for: .rootContainer)
NSOpenPanel
to ask user where to store the symbolic link to FileProvider root folder.NSOpenPanel
询问用户将符号链接存储到 FileProvider 根文件夹的位置。 Then store bookmark data of user-selected location and create symbolic link to FileProvider root within that location.let savePanel = NSOpenPanel()
savePanel.canChooseDirectories = true
savePanel.title = "Choose this location"
savePanel.prompt = "Choose"
let result = await savePanel.begin()
if result == NSApplication.ModalResponse.OK {
let panelURL = savePanel.url
let mountBookmarkData = try! panelURL.bookmarkData(options: .withSecurityScope)
UserDefaults.standard.set(mountBookmarkData, forKey: "aliasLocationBookmarkData")
let fileManager = FileManager.default
var aliasURL = panelURL.appendingPathComponent("MyRoot")
UserDefaults.standard.set(aliasURL.path, forKey: Constants.UserDefaults.aliasPath)
do {
try fileManager.createSymbolicLink(at: aliasURL, withDestinationURL: fileProviderFolderURL)
} catch {
// handle error
}
}
UserDefaults
and create URL from it.UserDefaults
中获取书签数据并从中创建 URL。 It is the location where symbolic link is stored.let aliasLocationBookmarkData = UserDefaults.standard.data(forKey: "aliasBookmarkData")!
var isStale = false
let aliasLocationURL = try URL(resolvingBookmarkData: aliasLocationBookmarkData, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale)
let securityScopeResult = aliasLocationURL.startAccessingSecurityScopedResource()
if securityScopeResult {
let aliasPathKey = Constants.UserDefaults.aliasPath
let aliasPath = UserDefaults.standard.string(forKey: aliasPathKey)
let aliasURL = URL(fileURLWithPath: aliasPath)
NSWorkspace.shared.open(aliasURL)
aliasLocationURL.stopAccessingSecurityScopedResource()
}
That's pretty much it.差不多就是这样。 The key is to store bookmark data of the location user picked in
NSOpenPanel
dialog, create symbolic link in it and then you'll be able to access that resource even after app relaunch.关键是存储用户在
NSOpenPanel
对话框中选择的位置的书签数据,在其中创建符号链接,然后即使在应用程序重新启动后您也可以访问该资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.