views:

154

answers:

2

I would like to create a helper application that has no GUI yet does have access to an app bundle. I was looking at the Xcode command line project templates, but these all just generate executable files. How can I create something like a command line tool that presents no application menu yet provides access to bundle resources in a .app? Am I thinking about this the wrong way?

Thanks.

+4  A: 

Peter's answer (use LSUIElement) is probably what you're looking for. Occasionally, I've wanted to create an actual command-line app that also has an app bundle. In case other's come across the same scenario, here's what I've found...

The app bundle is not an executable. The true executable is buried inside: MyApp.app/Contents/MacOS/MyApp is the actual Mach-O executable for an app named MyApp. The standard Xcode templates link applications against AppKit.framework and have a main.m that defines the main() function for the executable that starts an NSApplication instance. You can put any executable in an app bundle, however, including a console-only app. Just replace the main() function in main.m in the application template with your console apps main() function and remove AppKit.framework from the app's linked frameworks.

If you want, you can replace main.m with a main.c that declares a main function if you want to avoid Objective-C entirely (though there's nothing necessarily Objective-C specific in main.m if you replace the contents of the main() function).

To run your application from the console, you have to specify the actual executable, not the app bundle, so you'd have to type MyApp.app/Contents/MacOS/MyApp at the command line. When I've used app bundles for console apps (to provide linked frameworks, resources, etc.), I often create a symlink to the executable and put the symlink in /usr/local/bin (or somewhere else on the path) during installation or first run.

Barry Wark
All true enough, but Peter's answer may be closer to what the developer is trying to achieve.
Ken
@Ken: Agreed. The asker is probably looking for a LSUIElement app. I've found occasion to have a console-only app that uses an app bundle, however. Just in case it's useful to others, I'll leave this answer up.
Barry Wark
This was a helpful answer too, Barry, so thanks. The LSUIElement entry in Info.plist makes it really simple, though. Thanks again.
Matt Long
@Matt - Agreed; use LSUIElement. I'll edit my answer to make that clear and that the info is really only if you want to create a command-line app with an app bundle (yes, I've had to in the past).
Barry Wark
+5  A: 

I would like to create a helper application that has no GUI yet does have … an app bundle.

That's called an agent, and you make one by making a regular application and setting its LSUIElement key to the string “1”.

Peter Hosey