简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM