Archive for the ‘UNIX’ category

NSWorkspace Bug in – openFile: ?

December 7, 2007

EDITED TO ADD: I could reproduce this bug in a large project, but not isolate it in a smaller one. It is much likely for my code to have a bug then NSWorkspace. I’m still not 100% certain that this issue wasn’t my fault in some way I don’t understand. But no matter what caused the bug, the work-around I describe here has been working for me. Please let me know if you have this same issue as well, or any insight into what I could have done to cause it.

The Problem:
I need to launch a background-application from inside the application-support directory. The OS is Mac OS X 10.5.1 build 9B18.
[[NSWorkspace sharedWorkspace] openFile:path];
where path is the correct path to the application, in my case “/Users/user/Library/Application Support/IMLocation/IMLocationHelper.app.”, works about 3 out out 4 times, but intermittently fails. It also reports success without actually launching IMLocationHelper.app, but this is far more rare.

Details:
When openFile: explicitly fails, it prints, “LSOpenFromURLSpec() returned -600 for application (null) path /Users/user/Library/Application Support/IMLocation/IMLocationHelper.app.” to stderr.

Error -600 is procNotFound /*no eligible process with specified descriptor*/.

The path is correct, the application exists there. I verified this by
assert([[NSFileManager defaultManager] fileExistsAtPath:path]);
before calling openFile:.

Calling openFile: again immediately after a failure often works

Also, while(![[NSWorkspace sharedWorkspace] openFile:path]);
will terminate, meaning it reported success at some point, but it will not always have actually launched the application at path.

(EDITED TO ADD: At first I thought that, NSWorkspace was probably opening IMLocationHelper, but not blocking until it has finished being launched. But sleeping, to give IMLocationHelper time to finish launching, did not solve the problem.)

performSelectorOnMainThread did not solve anything ether.

I verify that IMLocationHelper has been launched by
assert([[ShellTask executeShellCommandSynchronously:@"ps -axww | grep IMLocationHelper | grep -v grep"] length] > 0);

A Workaround:
[[NSTask launchedTaskWithLaunchPath:@"/usr/bin/open" arguments:[NSArray arrayWithObject:path] waitUntilExit];.

This calls the open command to launch the application. I have not been able to make it fail when path exists.

Useful MacOSX Terminal Commands

November 16, 2007

This post has moved here.

Adding a Directory to $PATH in MacOSX

August 10, 2007

UPDATED FOR OS X 10.5

I don’t program in bash often enough to remember how to set shell-variables, so I always have to look up that :
PATH=$PATH:/extra/path1:extra/path2
is the line I need to add to my ~/.bashrc file, if I want to add /extra/path1:extra/path2 to my $PATH.

In fact, the reason for this post is because I had to look this up again tonight. The Title Case words in the title are the google query I used, but the results I found were too verbose for me. A year or two from now, when I have to look this up yet again, I’ll at least know where to look for a short answer (Update: actually it turned out to be almost exactly 6 months, not 1-2 years.)

Back when I first wrote this, I was running OS X 10.4, and I recommended modifying /etc/profile, because it was self-explanatory, and updated $PATH for all users. I no longer recommend this, because in OS X 10.5, $PATH is defined in /etc/profile as:
if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi

and I just don’t understand path_helper enough to know what damage not using it to update $PATH for the whole system could cause.

Determining your laptop’s location with a shell command

June 11, 2007

This article has been updated, and moved here