Redisplay在Finder中的图标视图(IKImageBrowserView)中更改了自定义图标

时间:2012-01-03 11:51:24

标签: objective-c cocoa nsworkspace

我正在使用NSWorkspace类的setIcon:forFile:options:方法来设置文件和目录的自定义图标。我的问题是,Finder在图标视图中没有反映图标更改,直到重新启动。

图标视图:
enter image description here

enter image description here

列表视图(大纲视图):
enter image description here

1 个答案:

答案 0 :(得分:1)

来自Carbon development tips and tricks

  

您需要向Finder发送一个kAESync AppleEvent并传递它   已更改的文件的别名。

下面的代码由JWWalker

撰写
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;
}