[Openmcl-devel] mac types and creators?

Gary Byers gb at clozure.com
Wed Jan 29 09:18:35 PST 2003

There isn't any way to do get at that stuff in the Darwin layer of
OSX (the SetFile tool is a command-line Carbon program.)

The question came up last spring, and I was able to write some
functions that used methods in the Cocoa Foundation classes to
get/set mac file type and creator information.  That code still
seems to work, and is enclosed.

The OSX Finder's "Get Info" dialog seems to offer a way (via the "Open
With ..." tab) a way to associate arbitrary files with specified
applications, and I believe that this association can be extemded to
other "documents like this" based in whole or in part on the file's
extension.  I don't know how to create such an association
programmatically (though I assume that there's a way to do so) and
don't know if/how such an association would make Quicktime accept
arbitrary .midi files.

If you have to step up a layer from Darwin to either set type/creator
info or to create the appropriate "association", you might consider
using NSWorkSpace's -openFile:withApplication: method (or something
equivalent in Carbon or CoreFoundation.) instead of running the
"open" program; I'd guess that "open" and -openFile:withApplication:
are doing pretty much the same thing internally.

On Wed, 29 Jan 2003, Rick Taube wrote:

> Hi, I'm running OPENMCL "Version (Beta: Darwin) 0.13.2" on OSX.
> I have a program that computes .midi files and I want to play them
> automatically after they are generated. Until I can figure out a better
> mechanism I am current using the ccl:run-program to do:
> 	open -a /Applications/Quicktime xxx.midi
> My problem is that Quicktime wont recognize xxx.midi as a midi file
> unless its "Mac File Type" is set. In MCL there is a function called
> ccl:set-mac-file-creator that I use to associate Quicktime with each
> .midi file that i generate.  Howver, neither ccl:set-mac-file-type nor
> ccl:set-mac-file-creator are defined in OpenMcl :(  I know I can use a
> developer tool SetFile to set the file type but people who use my
> program wont necessarily have access to this command.
> What is the best way to asscociate mac file creators and/or mac file
> types with a generated files from open mcl?
> thanks for any info
> Rick Taube
> Associate Professor, Composition/Theory
> School of Music
> University of Illinois
> Urbana, IL 61821 USA
> net: taube at uiuc.edu
> fax: 217 244 8319
> vox: 217 244 2684
> _______________________________________________
> Openmcl-devel mailing list
> Openmcl-devel at clozure.com
> http://clozure.com/cgi-bin/mailman/listinfo/openmcl-devel

-------------- next part --------------
(in-package "CCL")

(eval-when (:compile-toplevel :execute)
  (use-interface-dir :cocoa))

(eval-when (:compile-toplevel :load-toplevel :execute)
  (require "APPLE-OBJC"))

(eval-when (:compile-toplevel :execute)
  (setq *readtable* *objc-readtable*))

(defun %integer->ostype (integer)
  (%stack-block ((buf 4))
    (setf (%get-unsigned-long buf) integer)
    (%get-ostype buf)))

(defun %mac-type-and-creator (path new-type new-creator)
  (let* ((namestring (native-translated-namestring (pathname path))))  
    (with-cstrs ((cstr namestring))
      (with-nsstr (nsstr cstr (length namestring))
	(let* ((pool [[(@class "NSAutoreleasePool") "alloc"] "init"])
	       (type 0)
	       (creator 0))
		(let* ((fm [(@class "NSFileManager") "defaultManager"])
		       (dict [fm "fileAttributesAtPath:traverseLink:"
				 :id nsstr :<BOOL> #$YES :id]))
		  (if (%null-ptr-p dict)
		    (error "Can't get file information for ~s" path)
		    (let* ((typenum [dict "objectForKey:"
					 :id #@"NSFileHFSTypeCode"])
			  (creatornum [dict "objectForKey:"
					    :id #@"NSFileHFSCreatorCode"]))
		     (unless (%null-ptr-p typenum)
		       (setq type
			     [typenum "unsignedLongValue" :unsigned-fullword]))
		     (unless (%null-ptr-p creatornum)
		       (setq creator
			     [creatornum "unsignedLongValue" :unsigned-fullword]))
		     (when (or new-type new-creator)
		       (when new-type
			 (setq dict
				[(@class "NSNumber")
				 :unsigned-fullword new-type])))
		       (when new-creator
			 (setq dict
				[(@class "NSNumber")
				 :unsigned-fullword new-creator])))
		       [fm "changeFileAttributes:atPath:"
			   :id dict :id nsstr :<BOOL>])
		     (values (%integer->ostype (or new-type type))
			     (%integer->ostype (or new-creator creator))))))
	     [pool "release"])))))))

(defun mac-file-type (path)
  (nth-value 0 (%mac-type-and-creator path nil nil)))

(defun mac-file-creator (path)
  (nth-value 1 (%mac-type-and-creator path nil nil)))

(defun set-mac-file-type (path new-type)
  (nth-value 0 (%mac-type-and-creator path new-type nil)))

(defun set-mac-file-creator (path new-creator)
  (nth-value 1 (%mac-type-and-creator path nil new-creator)))

More information about the Openmcl-devel mailing list