Can you implement OS X's Finder download progr

2019-08-14 13:30发布

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?

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-14 14:23

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

查看更多
何必那么认真
3楼-- · 2019-08-14 14:34

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

查看更多
登录 后发表回答