您可以通过Shell脚本实现OS X的Finder下载进度条吗? [英] Can you implement OS X's Finder download progress bars from a shell script?

查看:154
本文介绍了您可以通过Shell脚本实现OS X的Finder下载进度条吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是否可以从命令行访问,还是只能从某些可可api内部访问?

解决方案

如果您不介意使用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

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?

解决方案

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

这篇关于您可以通过Shell脚本实现OS X的Finder下载进度条吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆