使用可可在finder中刷新文件或文件夹的图标

时间:2012-05-18 13:25:45

标签: cocoa applescript finder

我正在为Mac开发一个cocoa应用程序。我正在使用我的应用程序在文件和文件夹上应用叠加图标。但我的问题是,当我从我的应用程序更改文件或文件夹的图标而不是在Finder中没有反映它时,除非我点击Finder中的任何文件或文件夹。对于刷新Finder,我使用以下代码从我的应用程序运行以下AppleScript:

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

但是这段代码并没有让我的Finder更新。知道如何刷新Finder立即反映文件或文件夹的图标???提前谢谢......

4 个答案:

答案 0 :(得分:2)

在Finder中使用字符串时,必须指定类(文件夹,文件,磁盘,项目......),将适用于所有(文件夹,文件,... )。

没有它,它适用于某些人,但不是每个人都适用

此外,Finder中的“ posix file thePath ”在括号中更可靠。

试试这个

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

或没有 NSApplescript

[[NSWorkspace sharedWorkspace] noteFileSystemChanged: itemPath];

答案 1 :(得分:0)

@"tell application \"Finder\" to update POSIX file \"%@\""这个脚本对我来说很好。
您也可以使用苹果活动 下面的代码由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;
}

答案 2 :(得分:0)

以下是我在需要时“刷新Finder”的方法。也许它会对你有帮助;)

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

答案 3 :(得分:0)

对我有用的是“触摸”文件,该文件会更新文件修改日期,从而触发Finder刷新缩略图图标。

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