问题
At first I thought this might be some variation on the extended attributes that can be modified with the xattr command line tool. However, I've staged several tests, and the files don't seem to have any special attributes while in this mode.
Is this accessible at all from the command line, or is it only possible from within some cocoa api?
回答1:
If you don't mind scripting with swift:
#!/usr/bin/env swift
import Foundation
let path = ProcessInfo.processInfo.environment["HOME"]! + "/Downloads/a.txt"
FileManager.default.createFile(atPath: path, contents: nil, attributes: [:])
let url = URL(fileURLWithPath: path)
let progress = Progress(parent: nil, userInfo: [
ProgressUserInfoKey.fileOperationKindKey: Progress.FileOperationKind.downloading,
ProgressUserInfoKey.fileURLKey: url,
])
progress.kind = .file
progress.isPausable = false
progress.isCancellable = false
progress.totalUnitCount = 5
progress.publish()
while (progress.completedUnitCount < progress.totalUnitCount) {
sleep(1)
progress.completedUnitCount += 1
NSLog("progress %d", progress.completedUnitCount)
}
NSLog("Finished")
(Apple Swift version 4.1.2, Xcode 9.4)
Thanks to https://gist.github.com/mminer/3c0fbece956f3a5fa795563fafb139ae
回答2:
AFAIK, this all occurs through the NSProgress class, a Cocoa API, so getting it to happen from a shell script alone is very unlikely: https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSProgress_Class/#//apple_ref/doc/constant_group/File_operation_kinds
Here is how Chrome implemented it (newer code probably available): http://src.chromium.org/viewvc/chrome?revision=151195&view=revision
来源:https://stackoverflow.com/questions/26427421/can-you-implement-os-xs-finder-download-progress-bars-from-a-shell-script