2012-01-17 59 views
2

我想在Finder應用程序中刷新特定文件/文件夾的圖標。如何刷新查找窗口?

FNNotifyByPath((const UInt8 *)folderPath, kFNDirectoryModifiedMessage, kNilOptions); 

FNNotifyByPath對此不起作用。 現在我使用AppleScript

+(void) refreshIconForItem : (NSString *)itemPath 
{ 
    NSString *source=[NSString stringWithFormat:@"tell application \"Finder\" to update \"%@\"",[NSString stringWithUTF8String:itemPath]]; 
    NSAppleScript *update=[[NSAppleScript alloc] initWithSource:source]; 
    NSDictionary *err; 
    [update executeAndReturnError:&err]; 
} 

嘗試,但這功能也不能正常工作。

任何人都可以幫我嗎?

+0

你有沒有發現這個解決方案?請讓我知道哪些腳本正在爲此工作。因爲我使用了幾乎所有的腳本,但不會使用任何腳本。任何幫助表示讚賞..! – jigs 2015-10-28 11:47:45

+0

@Jigar你可以使用http://stackoverflow.com/a/15541439/944634。 Finder刷新蘋果腳本不能在10.8以上工作 – 2015-10-28 13:16:47

回答

5

您是否在executeAndReturnError:打電話後檢查了err字典的值?

正確的AppleScript語法是:

@"tell application \"Finder\" to update POSIX file \"%@\""

編輯補充:或者,你能下降到的AppleEvent級別:

OSStatus SendFinderSyncEvent(const FSRef* inObjectRef) 
{ 
    AppleEvent theEvent = { typeNull, NULL }; 
    AppleEvent replyEvent = { typeNull, NULL }; 
    AliasHandle itemAlias = NULL; 
    const OSType kFinderSig = 'MACS'; 

    OSStatus err = FSNewAliasMinimal(inObjectRef, &itemAlias); 
    if (err == noErr) 
    { 
     err = AEBuildAppleEvent(kAEFinderSuite, kAESync, typeApplSignature, 
      &kFinderSig, sizeof(OSType), kAutoGenerateReturnID, 
      kAnyTransactionID, &theEvent, NULL, "'----':alis(@@)", itemAlias); 

     if (err == noErr) 
     { 
      err = AESendMessage(&theEvent, &replyEvent, kAENoReply, 
       kAEDefaultTimeout); 

      AEDisposeDesc(&replyEvent); 
      AEDisposeDesc(&theEvent); 
     } 

     DisposeHandle((Handle)itemAlias); 
    } 

    return err; 
} 
+1

謝謝,它的工作正常。我應該使用executeAndReturnError:方法還是應該使用NSTask運行applescript? – 2012-01-17 13:41:21

+0

我會使用executeAndReturnError:或我添加的AppleEvent方式。 – JWWalker 2012-01-17 13:59:49

+0

謝謝AppleEvent代碼。你能否給我推薦AppleEvents的一些文檔?這樣我也可以學習這些東西。 – 2012-01-18 04:44:58