| Index | Submit class | Submit snippet | Submission feed | List |

Snippet #31

Language: Objective-C, Author: Zachary Schneirov
License: Public domain

Objective-C class method to set the invisibility flag of a file.

+ (BOOL)setInvisibilityFlag:(BOOL)invisible forPath:(NSString*)path {
  FSRef sourceRef;
  
  OSErr err = FSPathMakeRef((UInt8*)[path fileSystemRepresentation], &sourceRef, NULL);
  
  if (err == noErr) {
    FSCatalogInfo catalogInfo;
    
    /* get the current finderInfo */
    if ((err = FSGetCatalogInfo(&sourceRef, kFSCatInfoFinderInfo, &catalogInfo, NULL, NULL, NULL)) != noErr) {
      NSLog(@"FSGetCatalogInfo error: %d", err);
      return NO;
    }
    
    if (invisible) {
      /* OR in the bits */
      ((FileInfo *)&catalogInfo.finderInfo)->finderFlags |= kIsInvisible;
    } else {
      /* AND out the bits */
      ((FileInfo *)&catalogInfo.finderInfo)->finderFlags &= ~kIsInvisible;
    }
    
    /* save the modified finderInfo */
    if ((err = FSSetCatalogInfo(&sourceRef, kFSCatInfoFinderInfo, &catalogInfo)) != noErr) {
      NSLog(@"FSSetCatalogInfo error: %d", err);
      return NO;
    }
    
    return YES;
    
  } else {
    NSLog(@"error getting fsref from path %@: %d", path, err);
  }
  
  return NO;
}

Compatible with:


Comments

Name

Website

Do you hate spammers? (Answer "Yes")