[Openmcl-devel] The File Menu

Gary Byers gb at clozure.com
Sun Jan 24 00:53:56 PST 2010

The foreign pointer in question is likely a "selector", which is basically
a canonicalized C string that names an ObjC method.  (The fact that selectors
are just C strings is an artifact of Apple's ObjC implementation; you're
supposed to call #_sel_getName to get the C string associated with a selector;
#_sel_getName just returns its argument, currently.)

You can use the OBJC:@SELECTOR macro to obtain a selector, which is just
an opaque foreign pointer as far as the bridge can tell:

? (objc:@selector "foo")
#<A Foreign Pointer #x1374F0>
? (#_sel_getName *)
#<A Foreign Pointer #x1374F0>
? (%get-cstring *)

Using OBJC:@SELECTOR in lisp source code is preferable to using a raw
foreign pointer to the selector:  a selector's address can vary from
session to session and/or from OS release to OS release, and OBJC:@SELECTOR
handles that.

You can see or change the actions associated with menu items (and lots of other
things) in Interface Builder; you can also get and set actions procedurally.

(let* ((file-menu (grovel-around-a-bit))
        ;; There are several ways to find these menu items.
        (save-item (#/itemWithTitle: file-menu #@"Save"))
        (revert-item (#/itemWithTitle: file-menu #@"Revert")))
   (#/setAction: save-item (objc:@selector #/saveAndCommitDocument:))
   (#/setAction: revert-item (objc:@selector #/revertDocumentToLastCommittedVersion:)))

Some code - possibly including code which determines whether or not menu items
should be enabled - may look at menu items' actions in deciding this sort of
thing (e.g., "an item whose action is "saveDocument:" should be enabled if and only
if the frontmost window has a document associated with it and that document
has unsaved changes"); you may need to add or change some methods to handle
validation of the new selectors (if you do it this way.)

On Sat, 23 Jan 2010, Ron Garret wrote:

> Is it possible to change the behavior of items in the File menu?  Is it straightforward?  I want to intercept the SAVE and REVERT items and hook them up to a revision control system.  I tried to reverse engineer the menu but ran into a roadblock when the action selector was just a foreign pointer with no hint as to what it was pointing at.  Is the File menu managed by Lisp at all, or is all of its functionality provided by Cocoa?
> Thanks,
> rg
> _______________________________________________
> Openmcl-devel mailing list
> Openmcl-devel at clozure.com
> http://clozure.com/mailman/listinfo/openmcl-devel

More information about the Openmcl-devel mailing list