场景
用户在Finder中右键单击一个目录,并找到一个自定义菜单项。单击该项将告诉我的应用程序打开一个窗口,用户可以在其中完成工作。当用户完成后,需要将文件写入他通过右键单击选择的文件夹。
问题
我现在已经让所有的东西都工作了,但是最后一部分没有成功。扩展程序无法写入所选文件夹。
用户选择要交互的文件夹似乎不属于 Powerbox 的一部分,而 Powerbox 只有在使用 openPanel
和 savePanel
时才会激活 - 根据我的理解。那么如何获得与用户通过我的菜单项选择的文件夹进行交互的权限?我在开发者库中没有找到任何可能的解决方案的参考资料。在沙箱指南和扩展指南中都没有。
如果无法使用所选的文件和文件夹,则添加自定义菜单项的可能性将变得相当无用,因此我相信一定有一种方法可以访问它们。
也许我尝试写入的方式是错误的。我的主应用程序将临时文件写入共享组文件夹中。之后,它发送一个通知,扩展程序监听该通知:
func copyFile(notification:NSNotification)
{
NSLog("write file")
if let target = tmpTarget
{
let secureContainer = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.de.enie.Nu")
let contents = NSFileManager.defaultManager().contentsOfDirectoryAtURL(secureContainer!, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles | NSDirectoryEnumerationOptions.SkipsPackageDescendants | NSDirectoryEnumerationOptions.SkipsSubdirectoryDescendants , error: nil)
for content in contents as! [NSURL]
{
NSLog("tmp data: \(content.path!)")
if content.lastPathComponent!.stringByDeletingPathExtension == "SharedData"
{
NSLog("found shared file")
NSFileManager.defaultManager().copyItemAtURL(content, toURL: target.URLByAppendingPathComponent(content.lastPathComponent!), error: nil)
NSFileManager.defaultManager().removeItemAtURL(content, error: nil)
}
}
tmpTarget = nil
}
}
尝试写入文件会出现以下控制台通知:
- 在 /Users//Desktop/SharedData.png 上打开:操作不允许
- 拒绝文件写入创建 /Users//Desktop/SharedData.png
更新
我确认了自己没有任何错误。虽然我可以通过 NSOpenPanel 访问文件夹(这意味着权限应该是正确的),但我无法在 / 中创建文件夹,甚至无法为我的默认 FIFinderSyncController 的目标 URL 创建书签。