Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
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?
 
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]
		}
	}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.