[Openmcl-devel] programmatic file chooser?

Gary Byers gb at clozure.com
Tue Aug 26 10:25:48 UTC 2008

Enclosed is a very simple example showing one way to use NSOpenPanel.
It doesn't exercise all of NSOpenPanel's options or features, but
it does mention a few of them in passing and I think that this is
an example of "fairly typical" usage, and it seems to be ~10 lines of
code, for whatever that's worth.  (It seems to be safe to call it
from a listener.)

NSOpenPanel is a subclass of NSSavePanel (example left as an exercise.)
It may or may not be a little surprising that neither NSOpenPanel
nor NSSavePanel is mentioned directly in the IDE sources (which
obviously deal with Open and Save panels when ... opening and
saving documents.)  The editor sources (in "ccl:cococa-ide;cocoa-editor.lisp")
contain some methods which setup a popup menu which lists way too many
character encodings with often cryptic names; that menu is basically
used as the "accessory view" of an NSOpenPanel or NSSavePanel.

I would ordinarily point you to Apple's online documentation, but
the URL just changed and would probably change again by the time this
message reaches anyone.  Instead (and since it hasn't been plugged
in a while), it may be helpful to point out the AppKido program


AppKido's just a little program that browses locally-installed
copies of Apple's Cocoa documentation (and works well, unless
Apple changes the file:/ URL of those locally-installed copies.)
The information's exactly the same as what's in Apple's HTML
files, but it may be easier to navigate through that information
with AppKido than it would be elsewise), and it may be easier
to find what you're looking for (when you don't know what it's
called) via AppKido's navigation and search features.

(So, rather than saying RTFM!, I'm recommending what seems to
be a good tool for R'ing TFM.)

On Tue, 26 Aug 2008, Arthur W Cater wrote:

> Hi, I'd like to have an equivalent in ClozureCL (for Mac & Cocoa) of MCL's choose-file-dialog.
> Does such a thing exist already? or would I have to roll my own, and if so can
> anyone please point me to the names of useful cocoa classes & methods? I hope I don't have
> to go all the way to NSBrowser.
> Arthur
-------------- next part --------------
;;; Very simple example of using NSOpenPanel to select a single filename.
;;; A few utilities for dealing with NSStrings are defined (like most
;;; of the IDE) in the GUI package; they may be generally useful enough
;;; to be moved to OBJC or some other package (and exported !).

(defun choose-lisp-source-file ()
      (let* ((panel (#/openPanel ns:ns-open-panel))) ; allocate an NSOpenPanel
        (#/setAllowsMultipleSelection: panel nil) ; return at most one filename
        (when (eql #$NSOKButton
                   (#/runModalForDirectory:file:types: panel
                                                       +null-ptr+ ; default to last dir used
                                                       +null-ptr+ ; no preselected file
                                                       ;; An array containing an NSString
                                                       ;; describing the single file type
                                                       ;; we're interested in
                                                       (#/arrayWithObject: ns:ns-array
          ;; Because we told the panel to disallow multiple selection,
          ;; there should be exactly one object in this array, an
          ;; NSString describing the selected file.
          (let* ((files (#/filenames panel)))
            (if (eql 1 (#/count files))
              (gui::lisp-string-from-nsstring (#/objectAtIndex: files 0))
              (error "Don't know why we didn't get an NSArray containing exactly 1 file here.")))))))

More information about the Openmcl-devel mailing list