Search code examples
objective-ccocoansworkspace

Redisplay changed custom icon in Icon view(IKImageBrowserView) in Finder


I am using setIcon:forFile:options: method of NSWorkspace class for setting custom icon for file and directory. My problem is that , the Finder is not reflecting icon change in icon view until restart.

Icon view:
enter image description here

enter image description here

List view (outline view):
enter image description here


Solution

  • From Carbon development tips and tricks .

    You need to send a kAESync AppleEvent to the Finder passing it an alias to the file that was changed.

    Below code is written by 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;
    }