2010-06-02 16 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)