How can I check if a document isLocked in 10.7?
NSDocument has a method isLocked, but it available only on 10.8.
Here is my implementation:
+ (BOOL)isDocumentLocked:(NSDocument*)doc
{
if (doc == nil)
{
return NO;
}
else if ([doc respondsToSelector:@selector(isLocked)]) // 10.8
{
return [doc isLocked];
}
else // OS X version < 10.8
{
NSError * error;
BOOL isAutosavingSafe = [doc checkAutosavingSafetyAndReturnError:&error];
if (!isAutosavingSafe)
{
return YES;
}
if (doc.fileURL == nil)
return NO;
NSFileManager* fm = [NSFileManager defaultManager];
NSString* path = doc.fileURL.absoluteURL.path;
if (![fm isWritableFileAtPath:path])
return YES; // No writing permissions
NSDictionary *attributes = [fm attributesOfItemAtPath:path error:&error];
BOOL isLocked = [[attributes objectForKey:NSFileImmutable] boolValue];
if (isLocked)
{
return YES;
}
}
return NO;
}