Amrinder Singh
Amrinder Singh

Reputation: 215

Refresh icons for files or folders in finder using cocoa

I'm developing a cocoa application for Mac. I'm applying overlay icons on files and folders using my application. But my problem is that when I change icon for file or folder from my Application than its not reflecting it in the Finder unless I click on any file or folder in the Finder. For refreshing Finder I'm running following applescript using following code from my Application:

NSString *source=[NSString stringWithFormat:@"tell application \"Finder\" to update POSIX file \"%@\"",itemPath];
NSAppleScript *run = [[NSAppleScript alloc] initWithSource:source];
[run executeAndReturnError:nil];

But this code is not refreshing my Finder. Any idea how to refresh Finder to immediately reflect icon for file or folder??? Thanks in advance...

Upvotes: 1

Views: 2072

Answers (4)

mixtly87
mixtly87

Reputation: 1715

What worked for me is to 'touch' the file which updates the file modification date and thus triggers thumbnail icon refresh by the Finder.

NSString* path = "/Users/xx/...path_to_file"
NSString* command = [NSString stringWithFormat:@"touch \"%@\"", path];
int res = system(command.UTF8String);

Upvotes: 0

jackjr300
jackjr300

Reputation: 7191

You must specify the class (folder, file, disk, item, ...) when you use a string in the Finder, item will work for all (folder, file, ...).

Without it, It works for some, but it is not the case for everyone

Also, "posix file thePath" in the Finder is more reliable with parentheses.

Try this

NSString *source=[NSString stringWithFormat:@"tell application \"Finder\" to update item (POSIX file \"%@\")",itemPath];

Or without NSApplescript :

[[NSWorkspace sharedWorkspace] noteFileSystemChanged: itemPath];

Upvotes: 2

regulus6633
regulus6633

Reputation: 19030

Here's how I "refresh the Finder" when needed. Maybe it will help you ;)

tell application "Finder"
    tell window 1 to update items
end tell

Upvotes: 0

Parag Bafna
Parag Bafna

Reputation: 22930

@"tell application \"Finder\" to update POSIX file \"%@\"" this script working fine for me.
You can also use apple events.
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;
}

Upvotes: 0

Related Questions