In Cocoa, what is the purpose of the main.m class?
No, because he is not.
First, the .m file is not a class. Also no .h. The practice of placing the @interface class in the .h file (header) and its @implementation in the .m file (implementation) is an agreement, nothing more. This is a good agreement, but it is not respected by the language.
So, the .m file contains zero or more classes. Usually this is exactly one. Sometimes these are two, where the other is a small private class, whose @interface also in the same file.
main.m does not contain classes. Usually it contains only one function called main .
main comes from C (of which Objective-C is a superset) and is the entry point to the program: where the program starts. Everything happens here, or in a function called from here. The command line tool, as a rule, also exits from it, returning main .
Most of the main functions in Cocoa applications default to the project template; this implementation is simply tail-calls NSApplicationMain , which sets up the shared NSApplication object and starts it. This function never returns; when the user closes the Cocoa application, the process simply exits.
You can read this list of important facts about Cocoa and Objective-C that I wrote. It seems that you have some misconceptions (they may have moved from another structure with which you are more familiar) that this list may clarify you.
How to run code regardless of user interface actions (as, for example, not only in response to a button click)?
It depends on when you try to do this. For example, a periodic action would be a task for a timer .
I am trying to initialize some objects and run some scripts before I awakeFromNib. How to do it?
You could do this in initWithCoder: which is dispatched to each object without a visible layer created from the archive, including yours. Nibs are also archives.
You can also consider delegating the application and implement a applicationWillFinishLaunching: method .
The third way is to populate the code in main . Note that almost any Cocoa code here will probably register "no autorelease pool " unless you wrap it in the @autoreleasepool ; other solutions do not have this problem because NSApplication has already created an auto-resource pool for you.