views:

124

answers:

5

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;
}
A: 

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
Ief2
Did you run in an `ssh` session by logging in from another machine?
Yuji
A: 

You can see a working example of this script with applescript and also its source here:

macscripter.net topic 31683

Although I suspect you have seen this already.

markhunte
A: 
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.

Peter Hosey
+1  A: 
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.

Peter Hosey
Dear Peter, his code worked alright on my 10.6.2 Terminal.app. It might be that s/he ran it in the ssh session without the connection to the window server, which the `NSWorkspace` method might need. I ran the code on a headless old G4 machine, and it died because the window server can't be connected. (although the error was not the same as his/hers.)
Yuji
I've had similar problems running NSImage-powered command-line tools on the local machine. I don't think I've had problems simply creating one, which is weird, but I've definitely had the can't-create-window exception.
Peter Hosey
NSImage depends on AppKit, which AFAIK requires a window server connection.
Rob Keniger
AppKit does not always require a window server connection. NSWorkspace, for one, works fine in my experience. Prior to 10.6, NSImage does require a window server connection for many things, including compositing (locking focus on an image and drawing into it). As I said, though, I don't remember ever having this problem simply creating an image, so I do find that strange.
Peter Hosey
A: 

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.

Peter Zich