YuDenzel
YuDenzel

Reputation: 423

how to show a progress bar on file's icon

You know there is a mini progress bar on the file's icon when we copy a file or download file with safari and chrome.

I am wondering how to let it shown in Finder window when I'm copying or download files with my own code.

Could somebody help me?

Thanks a lot.

Upvotes: 6

Views: 3816

Answers (2)

YuDenzel
YuDenzel

Reputation: 423

I got the way by query file/directory attributes myself. It's quit simple.

Get attributes of the file you want to show a progress bar on it's icon

NSDictionary *fileAttr = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];

Get extend attributes

NSDictionary *extendAttr = [fileAttr objectForKey:@"NSFileExtendedAttributes"];

Create a Mutable copy of extendAttr and change some value in it

NSMutableDictionary *mutableExtendAttr = [NSMutableDictionary dictionaryWithDictionary:extendAttr];
// create data of progress value
NSData *progressData = [@"0.1" dataUsingEncoding:NSASCIIStringEncoding];
// change it
[mutableExtendAttr setObject:progressData forKey:@"com.apple.progress.fractionCompleted"];

Create a mutable copy of fileAttr

 NSMutableDictionary *mutableFileAttr = [NSMutableDictionary dictionaryWithDictionary:fileAttr];
 // change extend attr
 [mutableFileAttr setObject:[NSDictionary dictionaryWithDictionary:mutableExtendAttr] forKey:extendAttrKey];
 // change file/dir create date to the special one
 // if not , progress bar will not show
 [mutableFileAttr setObject:[NSDate dateWithString:@"1984-01-24 08:00:00 +0000"] forKey:NSFileCreationDate];
 // set attribute to file
 [[NSFileManager defaultManager] setAttributes:mutableFileAttr ofItemAtPath:filePath error:&error];

Now you'll find it the progress bar showed up.

Upvotes: 15

justin
justin

Reputation: 104698

One starting point would be to use a NSProgressIndicator, then [progressIndicator setIndeterminate:NO];. Your updates/background operation should then not block the main thread. NSProgressIndicator is a bit different for an AppKit widget -- it permits some updates from secondary threads. Of course, updates to the 'current progress' may also be enqueued upon the main thread's run loop.

Upvotes: 0

Related Questions