writeToFile With Hidden Bit Attributes?

Discussion in 'Mac Programming' started by Darkroom, Jan 4, 2009.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    for weeks i've been browsing the internet trying to learn how i can write a file that is hidden without having to prefix the file with "."... i know it's possible to simply name the file prefixed with a ".", but i'd like to know how to use what seems to be called "hidden bit"...

    Code:
    NSString *writeTextFile = CONSTnameAndLocationOnComputer;
    writeTextFile = [writeTextFile stringByExpandingTildeInPath];
    if ([fileManager fileExistsAtPath: writeTextFile] == NO)
    	{
    	NSString *theText = @"This is the file's text!";
    	[theText writeToFile:writeTextFile atomically:YES encoding:NSUnicodeStringEncoding error:NULL];
    	}
    
    what is it that i can add to my code so that the file is hidden by default in Finder?
     
  2. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #2
    i am following up with the answer.

    i found this snippet online that will make a file invisible... works great :)

    Code:
    + (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;
    	}	
    
    - (void)awakeFromNib
    	{
    	NSString *writeTextFile = @"~/Desktop/TextFile.txt";;
    	writeTextFile = [writeTextFile stringByExpandingTildeInPath];
    	
            if ([fileManager fileExistsAtPath: writeTextFile] == NO)
    		{
    		NSString *theText = @"This is the file's text!";
    		[theText writeToFile:writeTextFile atomically:YES encoding:NSUnicodeStringEncoding error:NULL];
                    [B][[self class] setInvisibilityFlag:YES forPath:writeTextFile];[/B]
    		}
    	}
    
     

Share This Page