Nili
Nili

Reputation: 2003

Cocoa NSWorkspace setIcon:forFile:options: doesn't work

I am copying a png file to some target folder, and then setting it's icon to some special icon. However, the icon doesn't change - it remains to be the preview of the png file. Is it possible to force icon replacement? Perhaps programatically disable the png's tendency to use the preview image?

What I am doing is:

[[NSFileManager defaultManager] copyItemAtPath:filename toPath:path error:nil];
NSImage* iconImage = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"myIcon" ofType:@"icns"]];
[[NSWorkspace sharedWorkspace] setIcon:iconImage forFile:path options:0];

P.S. - the above code works fine for files that don't have the preview feature (like files I created manually with my own special suffix).

Thanks, Nili

Upvotes: 0

Views: 831

Answers (1)

Nili
Nili

Reputation: 2003

Apparently, files that were copied [[NSFileManager defaultManager] copyItemAtPath:] are displaying their old icon that is generated using the quick look mechanism. Files that are created by my application and immediately get their icon by my application don't get interrupted by the quick look mechanism. My solution is not copying the file, but creating it myself and then everything works smoothly.

Would love to get a better answer/workaround though...

Upvotes: 0

Related Questions