v1Axvw
v1Axvw

Reputation: 3054

set file's icon in a command line utility not working

I just began to work with Objective-C and I'm managing pretty well. My last challenge was to make a command line utility, which I could than use in AppleScript. But my code does not work, not in the terminal, not in the AppleScript. So I'm asking you, what's the error in this peace of code, that should be very plain and easy?

int main(int argc, char *argv[]) {
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

 // -imagePath
 // -filePath
 NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
 NSString *soundPath = [args stringForKey:@"imagePath"];
 NSString *filePath = [args stringForKey:@"filePath"];

 BOOL worked = [[NSWorkspace sharedWorkspace] setIcon:[[NSImage alloc] initWithContentsOfFile:soundPath] forFile:filePath options:0];

 NSLog(@"Worked: %i",worked);

 [pool release];
 return 0;
}

Upvotes: 2

Views: 736

Answers (4)

Peter Zich
Peter Zich

Reputation: 512

I have an application that uses basically that code to set icons on OS X:

http://pzich.com/junk/Iconizer.app.zip

I'm not sure why yours isn't working.

Upvotes: 0

Peter Hosey
Peter Hosey

Reputation: 96333

2010-01-31 17:03:24.317 iConChange[14848:10b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error (1002) creating CGSWindow'

In effect, that means “You can't do that in a command-line tool”. If you run your tool in the debugger, it'll tell you what “that” is in the stack trace. My guess is that “that” is creating the NSImage.

Another solution is to rewrite the tool to use Icon Services instead of NSWorkspace. The APIs you'll need are still available and not deprecated.

Upvotes: 1

Peter Hosey
Peter Hosey

Reputation: 96333

2010-01-31 17:03:24.317 iConChange[14848:10b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error (1002) creating CGSWindow'

In effect, that means “You can't do that in a command-line tool”. If you run your tool in the debugger, it'll tell you what “that” is in the stack trace.

One solution is to rewrite it as an application. Perhaps a document-based one, with an image view in the document window, set up to receive dragged images and files.

Upvotes: 0

v1Axvw
v1Axvw

Reputation: 3054

Terminal:

macbook-van-ief2:~ ief2$ /Users/ief2/Desktop/iConChange/build/Debug/iConChange -filePath "~/Desktop/Naamloos.txt" -imagePath "~/Desktop/Z_Home_ZIcon.gif"
2010-01-31 17:03:24.311 iConChange[14848:10b] _NXCreateWindow: error setting window property (1002)
2010-01-31 17:03:24.317 iConChange[14848:10b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error (1002) creating CGSWindow'
2010-01-31 17:03:24.322 iConChange[14848:10b] Stack: (
    2459177131,
    2487344699,
    2459176587,
    2459176650,
    2441787103,
    2441786331,
    2441785537,
    2441784212,
    2441781861,
    2441794711,
    2441793509,
    2441762807,
    2444980701,
    2444978472,
    2447881218
)
Trace/BPT trap

Upvotes: 0

Related Questions