Search code examples
macosnsurlnsfilemanager

Getting the "locked" state of a file on macOS


One can lock a file against deletion, via the Finder's Get Info window.

I need to determine this lock state from within my program. I need a modern (64 bit savvy) C/ObjC/Swift API operation that lets me do that.


Solution

  • It's the NSURLIsUserImmutableKey resource key. Sneaky, because the documentation makes no mention of "lock".

    So, to get the lock state, use this code:

    - (BOOL)isLocked {
        NSNumber *result;
        if ([self.url getResourceValue:&result forKey:NSURLIsUserImmutableKey error:nil]) {
            return result.boolValue;
        }
        return NO;
    }