2010-06-02 38 views
4

我需要的,如果有可能使用的Objective-C或用C调用隐藏在聚光灯Finder中的文件,以及使在Finder中不可见的文件。如何使用Objective-C的

感谢

回答

4

您可以设置隐形属性通过一些C调用。这是相当原始的代码,只适用于某些文件系统,并且缺少错误检查。

#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; 
} 

或者你可以通过NSURL如果你可以针对雪豹它抽象掉了每个文件系统进程和处理扩展属性。

NSURL *url = [NSURL fileURLWithPath:path]; 
[url setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsHiddenKey error:NULL]; 
+0

感谢您的代码,我会尽可能在我的应用中实现代码。因为我需要照顾应用程序中的任何种类的不兼容。 – AmitSri 2010-06-15 07:58:59

3

(编辑:前面的点似乎并没有保持它mdfind的)“”

文件与开始将默认隐藏在Finder中。用户可以用defaults键覆盖该键,但这通常会处理它。

聚光灯,见TA24975,其中详细解释了什么Lyndsey提到。您可能需要结合这些方法,具体取决于您是否试图避免mdfind -name捡起它。

+0

谢谢,我想知道是否有任何搜索API可以使用设置隐藏标志。我正在搜索如何使用Finder API的标志kIsInvisible,以及如何使用设置particulatr文件的不可见标志。 – AmitSri 2010-06-03 08:20:41

+0

AmitSri:它是文件目录信息的一部分。使用'FSGetCatalogInfo'来获取文件的Finder'FileInfo'结构(在目录中,信息结构作为字节数组,出于某种原因宣布),设置隐形位和清除inited位,然后用'FSSetCatalogInfo'以应用更改。 – 2010-06-04 07:09:48

6

您可以使用:

chflags("/path/to/file", UF_HIDDEN); 

隐藏的任何文件。

更多见man chflags(2)