简体   繁体   English

如何以编程方式从 FileProvider Extension (macOS) 打开文件

[英]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 文件夹的示例:

  1. Retrieve FileProvider root folder URL:检索 FileProvider 根文件夹 URL:
let fileProviderFolderURL = try! await NSFileProviderManager(for: SREG.context.domain)?.getUserVisibleURL(for: .rootContainer)
  1. Use 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.然后存储用户选择位置的书签数据,并在该位置创建指向 FileProvider 根目录的符号链接。
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
    }

}
  1. Later, when you want to open the Root folder in Finder, fetch bookmark data from UserDefaults and create URL from it.稍后,当您想在 Finder 中打开 Root 文件夹时,从UserDefaults中获取书签数据并从中创建 URL。 It is the location where symbolic link is stored.它是存储符号链接的位置。 Start secure access on it and then open the symbolic link URL:开始对其进行安全访问,然后打开符号链接 URL:
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.

相关问题 如何在 macOS(不是 iOS)上的 SwiftUI 中以编程方式从后台打开应用程序 window? - How to programmatically open the app window from the background in SwiftUI on macOS (not iOS)? 如何在NSDocument macOS应用程序中以编程方式打开文档? - How to open programmatically a document in a NSDocument macOS app? 如何以编程方式打开Safari Extension ToolbarItem popover - How to open Safari Extension ToolbarItem popover programmatically macOS - 如何让NSSavePanel在文件名中添加文件扩展名? - macOS - How to have NSSavePanel to add a file extension in the file name? Swift 在 macOS 中以编程方式打开新窗口 - Swift open new window programmatically in macOS MacOS 上的 Swift:无法从我的应用程序打开文件 - Swift on MacOS: cannot open a file from my app 如何从自定义 macOS 文本共享扩展中获取文本? - How to get text from custom macOS text share extension? 如何在Swift中的macOS项目中获取当前打开文档的文件路径? - How to get file paths for current open documents in a macOS project in Swift? 如何从扩展程序中打开iOS应用程序? - How to open iOS app from within extension? 如何在 macOS 上从 SwiftUI 按钮打开 2 个窗口组? - How do you open 2 windowgroups from SwiftUI button on macOS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM