【问题标题】:Refresh icons for files or folders in finder using cocoa使用可可刷新finder中文件或文件夹的图标
【发布时间】:2012-05-18 13:25:44
【问题描述】:

我正在为 Mac 开发一个可可应用程序。我正在使用我的应用程序在文件和文件夹上应用覆盖图标。但我的问题是,当我从我的应用程序更改文件或文件夹的图标时,它不会在 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 以立即反映文件或文件夹的图标吗???在此先感谢...

【问题讨论】:

  • @Amirinder Singh 你找到解决这个问题的方法了吗?
  • @jigs 实际上我已经完成了那个项目并解决了这个问题。但是现在我没有源代码,所以不记得解决方案是什么。

标签: cocoa applescript finder


【解决方案1】:

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

没有它,它适用于某些人,但它并非适用于所有人

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

试试这个

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

或者没有 NSApplescript

[[NSWorkspace sharedWorkspace] noteFileSystemChanged: itemPath];

【讨论】:

  • 感谢您的回复。我尝试按照您的建议插入括号,但它不起作用。在用户单击 Finder 窗口之前,图标不会被反射。
  • 试试noteFileSystemChanged: --> [[NSWorkspace sharedWorkspace] noteFileSystemChanged: itemPath];
  • 我检查了这两个选项,但对我不起作用:(
【解决方案2】:

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

【讨论】:

  • 感谢您的回复。我在我的应用程序代码中复制了您提供的代码,但它为 kAEFinderSuite 提供了错误。您能否提供在我的应用程序中使用它所需的源代码以及如何在我的应用程序中调用此函数。
【解决方案3】:

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

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

【讨论】:

  • 感谢您的回复。您能否提供使用我的应用程序运行此脚本的源代码?
  • 只需将源代码的第一行更改为... NSString *source = @"tell application \"Finder\" to tell window 1 to update items"。
【解决方案4】:

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

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

【讨论】:

    猜你喜欢
    • 2015-07-24
    • 1970-01-01
    • 2014-12-14
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多