你能从shell脚本中实现OS X的Finder下载进度条吗?

时间:2014-10-17 14:38:13

标签: macos scripting finder xattr

首先,我认为这可能是可以使用xattr命令行工具修改的扩展属性的一些变体。但是,我已经进行了多次测试,在此模式下,这些文件似乎没有任何特殊属性。

这是否可以从命令行访问,或者只能从某些cocoa api中访问?

2 个答案:

答案 0 :(得分:1)

AFAIK,这一切都是通过NSProgress类(一个Cocoa API)实现的,因此单独从shell脚本中实现它是不太可能的: https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSProgress_Class/#//apple_ref/doc/constant_group/File_operation_kinds

以下是Chrome如何实现它(可能提供更新的代码): http://src.chromium.org/viewvc/chrome?revision=151195&view=revision

答案 1 :(得分:1)

如果您不介意使用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版本4.1.2,Xcode 9.4)

感谢https://gist.github.com/mminer/3c0fbece956f3a5fa795563fafb139ae

相关问题