[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Safe choose-file-dialog.



;Here is some code that provides a modicum of safety to opening files.  It
;circumvents the problem of ejected disks being remembered by MACL.

(defun safe-directory (directory)
"Tries to return each of the following, but
first checks that they are valid:
directory, *default-choose-file-directory*,
user-homedir-pathname, the first
valid device available.  If none of these are
valid, nil is returned."
  (cond
    ((probe-file (pathname-directory directory)) (pathname-directory directory))
    ((probe-file *default-choose-file-directory*)
     (pathname-directory *default-choose-file-directory*)
    )
    ((probe-file (user-homedir-pathname)) (user-homedir-pathname))
    (t (do* ((devices (devices) (cdr devices))
             (device (car devices) (car devices))
             (valid-p (probe-file device) (probe-file device))
            )
            ;test and result for do*
            ((or valid-p (null devices))
             (if valid-p
               device
               nil
            ))
            ;null body for do*
    )  )
) )

(defun safe-choose-file-dialog (&key (mac-file-type nil)
                                     (directory *default-choose-file-directory*)
                                     (button-string "Open")
                               )
"&key :mac-file-type :directory :button-string
Ensures that directory is valid with safe-directory,
and then displays the standard Macintosh
choose-file dialog, allowing the user to select a file which
already exists in the directory system."
  (do ((safe-dir (safe-directory directory) (safe-directory directory))
      )
      ;test and result for do
      (safe-dir
        (choose-file-dialog :mac-file-type mac-file-type
                            :directory safe-dir
                            :button-string button-string
      ) )
      ;null body for do.
) )

(defun safe-choose-new-file-dialog (&key (directory *default-choose-file-directory*)
                                         (prompt "AsI")
                                         (button-string "Open")
                                   )
"&key :directory :prompt :button-string
Ensures that directory is valid with safe-directory,
and then displays the standard Macintosh choose-new-file
dialog, allowing the user to select a new file in the
directory system."
  (do ((safe-dir (safe-directory directory) (safe-directory directory))
      )
      ;test and result for do
      (safe-dir
        (choose-new-file-dialog :directory safe-dir
                                :prompt prompt
                                :button-string button-string
      ) )
      ;null body for do.
) )