All Manuals > LispWorks Objective-C and Cocoa Interface User Guide and Reference Manual > 3 The Cocoa Interface

3.4 How to run Cocoa on its own

This section describes how you can run LispWorks as a Cocoa application, either by saving a LispWorks development image with a suitable restart function, or by delivering a LispWorks application which uses a nib file generated by Apple's Interface Builder.

3.4.1 LispWorks as a Cocoa application

The following startup function can be used to make LispWorks run as a Cocoa application. Typically, before calling "run" you would create an application delegate with a method on applicationDidFinishLaunching: to initialize the application's windows.

(defun init-function ()
  (mp:initialize-multiprocessing
   "main thread"
   '()
   #'(lambda ()
       (objc:ensure-objc-initialized
        :modules
        '("/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation"
          "/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa"))
       (objc:with-autorelease-pool ()
         (let ((app (objc:invoke "NSApplication"
                                 "sharedApplication")))
           (objc:invoke app "run"))))))

To use this, a bundle must be created, calling init-function on startup. For example, the following build script will create lw-cocoa-app.app:

(in-package "CL-USER")
(load-all-patches)
(example-compile-file
 "configuration/macos-application-bundle.lisp" :load t)
(save-image (when (save-argument-real-p)
              (write-macos-application-bundle "lw-cocoa-app"))
            :restart-function 'init-function)

See "Saving a LispWorks image" in the LispWorks® User Guide and Reference Manual for information on using a build script to create a new LispWorks image.

3.4.2 Using a nib file in a LispWorks application

For a complete example demonstrating how to build a standalone Cocoa application which uses a nib file, see these two files:

(example-edit-file "objc/area-calculator/area-calculator")
(example-edit-file "objc/area-calculator/deliver")

The area calculator example connects the nib file generated by Apple's Interface Builder to a Lisp implementation of an Objective-C class which acts as the MVC controller.


LispWorks Objective-C and Cocoa Interface User Guide and Reference Manual - 01 Dec 2021 19:38:33