I need to hide the file in finder as well as in spotlight if possible using objective-c or using C calls.
Thanks
I need to hide the file in finder as well as in spotlight if possible using objective-c or using C calls.
Thanks
(EDIT: Leading dot doesn't seem to keep it out of mdfind)
Files that begin with a "." will be hidden in Finder by default. Users can override that with a defaults
key, but this will take care of it in general.
For Spotlight, see TA24975, which explains in more detail what Lyndsey mentions. You probably need to combine the approaches, depending on whether you're trying to avoid mdfind -name
from picking it up.
You can set the invisibility attribute through some C calls. This is pretty raw code that only works on some file systems and is missing error checking.
#include <assert.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <sys/attr.h>
#include <sys/errno.h>
#include <unistd.h>
#include <sys/vnode.h>
typedef struct attrlist attrlist_t;
struct FInfoAttrBuf {
u_int32_t length;
fsobj_type_t objType;
union {
char rawBytes[32];
struct {
FileInfo info;
ExtendedFileInfo extInfo;
} file;
struct {
FolderInfo info;
ExtendedFolderInfo extInfo;
} folder;
} finderInfo;
};
typedef struct FInfoAttrBuf FInfoAttrBuf;
static int SetFileInvisibility(const char *path, int isInvisible) {
attrlist_t attrList;
FInfoAttrBuf attrBuf;
memset(&attrList, 0, sizeof(attrList));
attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
attrList.commonattr = ATTR_CMN_OBJTYPE | ATTR_CMN_FNDRINFO;
int err = getattrlist(path, &attrList, &attrBuf, sizeof(attrBuf), 0);
if (err != 0)
return errno;
// attrBuf.objType = (VREG | VDIR), inconsequential for invisibility
UInt16 flags = CFSwapInt16BigToHost(attrBuf.finderInfo.file.info.finderFlags);
if (isInvisible)
flags |= kIsInvisible;
else
flags &= (~kIsInvisible);
attrBuf.finderInfo.file.info.finderFlags = CFSwapInt16HostToBig(flags);
attrList.commonattr = ATTR_CMN_FNDRINFO;
err = setattrlist(path, &attrList, attrBuf.finderInfo.rawBytes, sizeof(attrBuf.finderInfo.rawBytes), 0);
return err;
}
or you can go through NSURL
if you can target Snow Leopard which abstracts away how each file system processes and handles extended attributes.
NSURL *url = [NSURL fileURLWithPath:path];
[url setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsHiddenKey error:NULL];