[Pythonmac-SIG] Method to lock/unlock files

Mike Nardell miken@inetworld.net
Fri, 10 May 2002 15:20:01 -0700

Hi -->
Last month I was having trouble locking or unlocking files from within a
Python program. By locking, I mean write protecting the file so that is put
into a read-only state, and unlocking I mean making the file write/readable.
Typically I accomplish this by using the Finder Info dialog on a file, and
clicking in the Locked check box. From the documentation, it seemed like the
best approach would involve the macfs module. In particular setting the
Flags field in the FInfo object. For some reason this does not work. I am
able to change other aspects of the file by changing the value of the Flags
field of a FInfo object... just not the lock/unlock state.

I started working on this problem again, and really have not had any luck
getting anything to work with functions found in the macfs module. I tried a
few things with the Carbon.Res module with no success (perhaps due to my
ignorance of the Carbon modules.)

It seems like a potential work-around would be to use the approach
demonstrated in the findertools.py module: use the interface to the Finder
found in the lib-scriptpackages. For instance, there is a function in the
findertools.py that sets the comment field for a file, and another function
changes a file's icon location in a window. However I just can't quite wrap
my head around the interface to the Finder. It seems that controlling the
Finder from Python is a very powerful technique, however it is one that is
not straight-forward.

If there is anyone who can give me guidance in this area, that would be
greatly appreciated.



> Yes, what I have observed is that setting the Flags field of the FInfo struct
> does not seem to have much effect on the lock/unlock status of the file. To
> clarify, after I set the the value of the Flags field of the FInfo, I use the
> SetFInfo method of the FSSpec to provide the file with the new value. After
> doing this, the change is not reflected in the Finder Info Window, and I am
> able to issue the following command from the Python Interpreter without
> throwing an exception:>>> file = open ("someTestFile", 'w')  [ where
> "someTestFile" is the file that I set the flag values on]
> My next step will be to try to do this file locking by using a Carbon module.
> I down-loaded the _Macintosh C_ box you recommended from the MacTech site.
> Thanks for your advice on this; I look forward to finding some solution.
> Regards,
> Michael Nardell
> ------------------------------------------
> "Colorless green ideas sleep furiously." -- Noam Chomsky
> ------------------------------------------
> on 4/26/02 1:53 AM, Jack Jansen at Jack.Jansen@cwi.nl wrote:
>> This is strange. The "256" sounds reasonable (look at the top of
>> :Mac:Lib:MACFS.py for the bits in the Flags field), but it should change
>> if you set the lock bit (4096 should be added). You should however call
>> GetFInfo() again, of course, the structure isn't updated live.
>> To make matters worse all this doesn't seem to work on OSX. I always get
>> "0" back in the flags, and I can set whatever I want without effect. And
>> I don't think this is a Python problem: xFiles (a utility to look at
>> mode bits and finfo bits) also doesn't see changes made in the finder
>> info window...
>> --
>> - Jack Jansen        <Jack.Jansen@oratrix.com>
>> http://www.cwi.nl/~jack -
>> - If I can't dance I don't want to be part of your revolution -- Emma
>> Goldman -