2

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?

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
  • have you found this solution? Please let me know which script is working for doing this. Because i used almost all the script but won't work any script. Any help is appreciated..!! – jigs Oct 28 '15 at 11:47
  • @Jigar you can use http://stackoverflow.com/a/15541439/944634. Finder refresh apple script is not working on 10.8 and above – Parag Bafna Oct 28 '15 at 13:16

1 Answers1

5

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;
}
JWWalker
  • 22,385
  • 6
  • 55
  • 76
  • 1
    Thank you, its working fine. should i use executeAndReturnError: method or should i run applescript using NSTask? – Parag Bafna Jan 17 '12 at 13:41
  • I'd use executeAndReturnError:, or the AppleEvent way I added. – JWWalker Jan 17 '12 at 13:59
  • Thank you for AppleEvent code. could you please suggest me some documentation for AppleEvents ? so that i can also learn this things. – Parag Bafna Jan 18 '12 at 04:44
  • Here's one: [Apple Events Programming Guide](http://developer.apple.com/legacy/mac/library/#documentation/AppleScript/Conceptual/AppleEvents/intro_aepg/intro_aepg.html) I don't know why it's marked Legacy. The `NSAppleEventDescriptor` class reference suggests looking at this guide. – JWWalker Jan 18 '12 at 06:44
  • [How to refresh browser view in finder window(mac os 10.5)?](http://stackoverflow.com/q/9096277/944634) – Parag Bafna Feb 01 '12 at 13:08
  • This function is not releasing memory. when i am copying 1k files and refreshing the finder, my application memory is going 300MB. and its not going down. – Parag Bafna Feb 09 '12 at 16:59
  • @ParagBafna: You can see that the function releases all the memory that it explicitly allocates. But it is possible that the first time you build or send an Apple Event, the OS might load some code or create a data structure that it never removes. That's not necessarily an error. – JWWalker Feb 10 '12 at 05:26
  • 1
    tell application \"Finder\" to update every item in front window is not working in 10.8. – Parag Bafna Aug 03 '12 at 11:15
  • 1
    I have a a app bundle on my desktop. Its path is "/Users/noi/Desktop/noits.app" but this apple script code is not working: `"tell application "Finder" to update POSIX file "/Users/noi/Desktop/noits.app"` im very new to this scripting – Noitidart Jan 12 '15 at 23:04
  • FWIW- The AppleEvent technique has worked great for years, but under 10.13 High Sierra it seems to cause the Finder to delete both the custom icon and the FinderInfo attribute. On the plus side, the Finder seems to refresh just fine without notification so with a quick call to Gestalt() the notification can just be skipped. – Seth Noble Jan 11 '18 at 21:44