PDA

View Full Version : writeToFile With Hidden Bit Attributes?




Darkroom
Jan 5, 2009, 12:51 AM
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"...


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?



Darkroom
Jan 5, 2009, 09:04 AM
i am following up with the answer.

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


+ (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];
[[self class] setInvisibilityFlag:YES forPath:writeTextFile];
}
}