Last active
June 5, 2019 16:00
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Disregard this, look at the fork at https://gist.github.com/amomchilov/7a977b9f99e898f0ff5d8e500c213aef