【问题标题】:Can you implement OS X's Finder download progress bars from a shell script?你能从 shell 脚本中实现 OS X 的 Finder 下载进度条吗?
【发布时间】:2014-10-17 14:38:12
【问题描述】:

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

这完全可以从命令行访问,还是只能从一些可可 api 中访问?

【问题讨论】:

    标签: macos scripting finder xattr


    【解决方案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

    【讨论】:

    • 老实说,从来没有想过这个问题会被回答。非常棒。
    【解决方案2】:

    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

    【讨论】:

      猜你喜欢
      • 2011-01-26
      • 2010-09-21
      • 2014-09-19
      • 2016-03-06
      • 2011-06-03
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多