Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Displays a progress indicator on a file in the 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

This comment has been minimized.

Copy link
Owner Author

amomchilov commented Jun 6, 2019

Slight modification for continued animation:

// Simulate downloading a file.
let timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { _ in
	if progress.completedUnitCount + 1 == progress.totalUnitCount {
		progress.completedUnitCount = 0
	}

	progress.completedUnitCount += 1
	print("\(progress.completedUnitCount) / \(progress.totalUnitCount)")
}

screencast 2019-06-06 00-36-00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.