Trygve
Trygve

Reputation: 1387

Tell Finder to copy/move programmatically

I have a list of file URLs that are in different folders and volumes. I want to tell Finder to copy or move them them from within a cocoa app.

It should default to move, but use copy if the volume of the source and destination are different. I want to end up with the Finder displaying its progress window.

Is there a way to do this? When I use drag and drop I can get it to copy like this, but that requires the user to do it. I want the user to select a destination folder in a standard file dialog sheet and then have it start.

AppleScript seems a messy and error-prone way to do it.

Upvotes: 0

Views: 151

Answers (1)

toadzky
toadzky

Reputation: 3846

Deciding whether or not to move or copy is up to your app logic. To move a file in Swift 4, do this:

do {
  try FileManager.default.moveItem(atPath: sourcePath, toPath: destinationPath)
  return nil
} catch let err {
  // Do something with the error
}

Copying looks almost exactly the same way:

do {
  try FileManager.default.copyItem(atPath: sourcePath, toPath: destinationPath)
  return nil
} catch let err {
  // do something with error
}

Upvotes: 0

Related Questions