gpt4 book ai didi

macos - 如何在文件图标上显示进度条

转载 作者:行者123 更新时间:2023-12-03 16:08:15 28 4
gpt4 key购买 nike

您知道,当我们使用 safari 和 chrome 复制文件或下载文件时,文件图标上有一个迷你进度条。

我想知道当我使用自己的代码复制或下载文件时如何让它显示在 Finder 窗口中。

有人可以帮助我吗?

非常感谢。

最佳答案

我自己通过查询文件/目录属性得到了方法。很简单。

获取要在其图标上显示进度条的文件的属性

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

获取扩展属性

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

创建extendAttr的可变副本并更改其中的某些值

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"];

创建 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];

现在你会发现进度条出现了。

关于macos - 如何在文件图标上显示进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15018052/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com