views:

76

answers:

2

I need to hide the file in finder as well as in spotlight if possible using objective-c or using C calls.

Thanks

+2  A: 

(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.

Rob Napier
Thanks, i am wondering is there any Finder API can be use to set the hidden flag. i was searching how to use the Finder API's flag kIsInvisible and how can be use to set the invisible flag for particulatr file.
AmitSri
AmitSri: It's part of the file's catalog info. Use `FSGetCatalogInfo` to get the file's Finder `FileInfo` structure (declared in the catalog-info structure as an array of bytes, for some reason), set the invisible bit and clear the inited bit, then use `FSSetCatalogInfo` to apply that change.
Peter Hosey
+1  A: 

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];
Ashley Clark
Thanks for the code,I'll try to implement the code in my app if possible. As i need to take care of any kind incompatibility in the app.
AmitSri