[Pythonmac-SIG] Creating resource files

Michael Hudson mwh@python.net
Wed, 19 Mar 2003 10:55:37 +0000


Jack Jansen <Jack.Jansen@oratrix.com> writes:

> On dinsdag, maa 18, 2003, at 15:45 Europe/Amsterdam, Michael Hudson
> wrote:
>
>> So, I have a path, a creator code and a type code.  I want to create a
>> file with a resource fork.  This is the code I have now:
>>
>>                 # this cannot possibly be the easiest way of doing
>> this.
>>                 dir = os.path.dirname(path)
>>                 if not dir:
>>                     dir = os.curdir
>>                 volID = File.FSSpec(dir).as_tuple()[0]
>>                 dirRef = File.FSRef(dir)
>>                 ci = dirRef.FSGetCatalogInfo(Files.kFSCatInfoNodeID)
>>                 dirID = ci[0].nodeID
>>                 newSpec = File.FSSpec((volID, dirID,
>>                                        os.path.basename(path)))
> Everything up to here can be done as
>                   newSpec = File.FSSpec(path)

That would be nice, if true:

->> File.FSSpec("/not-there")
Traceback (most recent call last):
  File "<input>", line 2, in ?
Error: (-43, 'File not found')

Should I update again?  AIUI, File.FSSpec(path) goes through creating
a FSRef, and you can't have a FSRef to a file that doesn't exist (it
seems, anyway).

>>                 newSpec.FSpCreate(CREATOR, TYPE, 0)
>>                 Res.FSpCreateResFile(path, CREATOR, TYPE, 0)
>
> As to the FSRef calls: setting creator/type indeed seems to be
> difficult with these. I think it can (in C) be done with
> FSSetCatalogInfo, but I'm not sure whether all the needed bits are
> exported to Python (I did a quick and dirty implementation of
> FS{Get,Set}CatalogInfo only a couple of weeks ago). If there are bits
> missing please let me know.

It wasn't obvious to me how to use FSSetCatalogInfo to set creator and
file type in C...

> Hmm, you could use the FSRef calls to create the resource file,
> something like (off the top of my head)
> 	dstdir, filename = os.path.split(path)
> 	fsr, fss = Res.CreateResourceFile(dstdir, unicode(filename,
> 	'utf8'), File.FSGetResourceForkName())
> 	MacOS.SetCreatorAndType(fsr, CREATOR, TYPE)

->> Res.CreateResourceFile
Traceback (most recent call last):
  File "<input>", line 2, in ?
AttributeError: 'module' object has no attribute 'CreateResourceFile'

Hmm, Res.FSCreateResFile looks hopeful...

> Pathnames, FSSpecs and FSRefs can be used interchangeably almost
> everywhere. the only reason for the unicode(filename, 'utf8') is that
> if you pass a string where the PyArg_Parse format expects a unicode it
> will just pas the raw data. Very handy for writing unicode data to
> files, very stupid for places where the unicode is actually
> interpreted (like filenames).

Yeah, I knew about these issues...

Thanks for the help!

Cheers,
M.

-- 
  For every complex problem, there is a solution that is simple,
  neat, and wrong.                                    -- H. L. Mencken