2013-02-22 65 views
4

當我們使用safari和chrome複製文件或下載文件時,您知道文件圖標上有一個迷你進度條。如何在文件圖標上顯示進度條

我想知道如何讓它顯示在Finder窗口中,當我用自己的代碼複製或下載文件時。

有人能幫助我嗎?

非常感謝。

回答

11

我自己通過查詢文件/目錄屬性得到了方法。它很簡單。

文件的獲取屬性要在它的圖標顯示一個進度條

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

現在你會發現它的進度條出現了。

+0

更新:似乎chrome的下載文件名以.crdownload結尾,並沒有使用這種方法,我想知道如何使用chrome。 – YuDenzel 2013-03-05 05:22:29

+0

僅適用於Lion(10.7)還是10.8? – UJey 2013-04-22 17:17:31

+0

@UJey我只測試了OSX 10.8,因爲我只有一個10.8的Mac mini。 – YuDenzel 2013-05-03 09:28:24

0

一個起點是使用NSProgressIndicator,然後[progressIndicator setIndeterminate:NO];。你的更新/後臺操作應該是而不是阻止主線程。對於AppKit小部件,NSProgressIndicator有點不同 - 它允許從輔助線程更新一些。當然,對'當前進度'的更新也可以在主線程的運行循環中排隊。

+0

我不認爲你已經得到了我上面描述的。例如,當您將大文件(如1GB文件)複製到新目錄時,它不會立即完成,然後查看目錄中的新文件,其圖標下方有進度欄。我感興趣的是當我用我的代碼編寫文件時如何顯示進度條(在文件圖標下)。 – YuDenzel 2013-02-26 02:37:36

+0

供參考:你可以在我的答案中找到一些信息,如果你有興趣@justin – YuDenzel 2013-02-27 13:07:02