Search code examples
objective-ccocoaapplescriptmacos-carbonfinder

How to refresh finder window?


I want to refresh icon for particular file/folder in Finder application.

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

FNNotifyByPath is not working for this. Now i am trying with 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];
}

but this function is also not working.

Can anyone please help me out?


Solution

  • Did you check the value of the err dictionary after the executeAndReturnError: call?

    The correct AppleScript syntax would be:

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

    EDIT TO ADD: Alternately, you could drop down to the AppleEvent level:

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