【问题标题】:How to make a file invisible in Finder using objective-c如何使用objective-c在Finder中使文件不可见
【发布时间】:2010-06-02 15:07:41
【问题描述】:

如果可能,我需要使用objective-c 或使用C 调用将文件隐藏在finder 和聚光灯下。

谢谢

【问题讨论】:

    标签: objective-c cocoa macos file


    【解决方案1】:

    你可以使用:

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

    隐藏任何文件。

    请参阅man chflags(2) 了解更多信息。

    【讨论】:

      【解决方案2】:

      您可以通过一些 C 调用来设置 invisibility 属性。这是非常原始的代码,仅适用于某些文件系统并且缺少错误检查。

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

      或者,如果您可以针对 Snow Leopard,您可以通过 NSURL,它抽象出每个文件系统如何处理和处理扩展属性。

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

      【讨论】:

      • 感谢代码,如果可能的话,我会尝试在我的应用程序中实现代码。因为我需要处理应用程序中的任何不兼容问题。
      【解决方案3】:

      (编辑:前导点似乎无法将其排除在 mdfind 之外)

      以“.”开头的文件默认情况下将隐藏在 Finder 中。用户可以使用 defaults 键覆盖它,但这通常会处理它。

      对于 Spotlight,请参阅 TA24975,它更详细地解释了 Lyndsey 提到的内容。您可能需要结合这些方法,具体取决于您是否试图避免 mdfind -name 选择它。

      【讨论】:

      • 谢谢,我想知道是否有任何 Finder API 可用于设置隐藏标志。我正在搜索如何使用 Finder API 的标志 kIsInvisible 以及如何使用来为特定文件设置不可见标志。
      • AmitSri:它是文件目录信息的一部分。使用FSGetCatalogInfo 获取文件的Finder FileInfo 结构(在catalog-info 结构中声明为字节数组,出于某种原因),设置不可见位并清除初始化位,然后使用FSSetCatalogInfo 应用它改变。
      猜你喜欢
      • 2015-03-30
      • 2014-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多