【问题标题】:Tell Finder to copy/move programmatically告诉 Finder 以编程方式复制/移动
【发布时间】:2018-02-11 09:16:47
【问题描述】:
我有一个位于不同文件夹和卷中的文件 URL 列表。我想告诉 Finder 从可可应用程序中复制或移动它们。
它应该默认移动,但如果源和目标的卷不同,则使用复制。我想最终让 Finder 显示其进度窗口。
有没有办法做到这一点?当我使用拖放时,我可以像这样复制它,但这需要用户这样做。我希望用户在标准文件对话框表中选择一个目标文件夹,然后启动它。
AppleScript 似乎是一种混乱且容易出错的方式。
【问题讨论】:
标签:
cocoa
copy
move
nsfilemanager
finder
【解决方案1】:
决定是移动还是复制取决于您的应用逻辑。要在 Swift 4 中移动文件,请执行以下操作:
do {
try FileManager.default.moveItem(atPath: sourcePath, toPath: destinationPath)
return nil
} catch let err {
// Do something with the error
}
复制看起来几乎完全相同:
do {
try FileManager.default.copyItem(atPath: sourcePath, toPath: destinationPath)
return nil
} catch let err {
// do something with error
}