John O
John O

Reputation: 5473

Can you implement OS X's Finder download progress bars from a shell script?

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?

Upvotes: 2

Views: 1031

Answers (3)

Matt Sephton
Matt Sephton

Reputation: 4142

You can do this in plain shell script using the xattr command.

xattr -w com.apple.progress.fractionCompleted 0.654 file.ext

If you inspect the xattr of an in-progress download you'll see it, here I'm using the xattred app:

view of progress xattr value

Upvotes: 1

seb
seb

Reputation: 2385

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

Upvotes: 3

Jnetz
Jnetz

Reputation: 11

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

Upvotes: 1

Related Questions