NSWorkspace SetIcon not updating application icon on El Capitan

狂风中的少年 提交于 2021-01-27 23:41:42

问题


I am using NSWorkSpace SetIcon:forFile:options: for changing application icon. On Yosemite it is working fine. However on El Capitan application icon is not updating.


回答1:


You are not alone. Try to set nil icon first and then your icon:

[[NSWorkspace sharedWorkspace] setIcon:nil forFile:path options:0]
[[NSWorkspace sharedWorkspace] setIcon:image forFile:path options:0];



回答2:


I was facing the same problem on OSx 10.14.6. I use the workaround where we set the icon after a delay after creating the file. Immediately setting the icon was working sometimes and not refreshing sometimes despite returning success.

    auto nspath = [NSString stringWithUTF8String:path.c_str()];
    auto nsdata = [contents dataUsingEncoding:NSUTF8StringEncoding];
    [[NSFileManager defaultManager] removeItemAtPath:nspath error:nil];
    auto ret = [[NSFileManager defaultManager] createFileAtPath:nspath contents:nsdata attributes:nil];
    // hide the extension of the file after creation.
    if (ret) {
        [[NSFileManager defaultManager] setAttributes:@{NSFileExtensionHidden: @true} ofItemAtPath:nspath error:nil];
        // set the icon, after a delay. Bug in OSx, the icon is not reflected sometimes despite YES return
        // if it is set immediately after creating the file.
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
            auto img = [NSImage imageNamed:@"Icon"];
            [[NSWorkspace sharedWorkspace] setIcon:img forFile:nspath options:0];
        });
    }


来源:https://stackoverflow.com/questions/33608339/nsworkspace-seticon-not-updating-application-icon-on-el-capitan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!