2012-01-17 38 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