Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amomchilov/7a977b9f99e898f0ff5d8e500c213aef to your computer and use it in GitHub Desktop.
Save amomchilov/7a977b9f99e898f0ff5d8e500c213aef to your computer and use it in GitHub Desktop.
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
Copy link
Author

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