Skip to content

Instantly share code, notes, and snippets.

@amomchilov
Last active June 5, 2019 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amomchilov/53275fcda8f945dbdd67f3c1b8fd82df to your computer and use it in GitHub Desktop.
Save amomchilov/53275fcda8f945dbdd67f3c1b8fd82df to your computer and use it in GitHub Desktop.
An example of using (NS)Progress to show a progress bar in Finder
import Cocoa
let path = "/Users/You/Pick/Any/Random/File/On/Your/System.txt"
let destination = URL(fileURLWithPath: path)
let progress: Progress = {
let p = Progress(parent: nil, userInfo: [
.fileOperationKindKey: Progress.FileOperationKind.downloading,
.fileURLKey: destination,
])
p.isCancellable = true
p.isPausable = false
p.kind = .file
p.totalUnitCount = 10
p.publish()
return p
}()
// Simulate downloading a file.
let timer = Timer.scheduledTimer(withTimeInterval: 0.25, repeats: true) { _ in
progress.completedUnitCount += 1
print("\(progress.completedUnitCount) / \(progress.totalUnitCount)")
if progress.completedUnitCount == progress.totalUnitCount {
Darwin.exit(0)
}
}
RunLoop.current.run()
@amomchilov
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment