2010-01-05 42 views
10

通過終端可以用一個命令使它 - 「SetFile -a乙文件名」化妝MAC包/捆編程

編程我想應該設定的屬性中的一個通過 [[的NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:NO attributes:attributes error:nil];

但我找不到哪一個。

謝謝

回答

12

能夠以編程方式設置束位仍然很有用,例如iPhoto會這樣做,以使iPhoto Library文件夾顯示爲單個文件。

您可以使用Carbon File Manager API以編程方式設置束位。您需要確保您的應用程序鏈接到Carbon框架並導入<Carbon/Carbon.h>標題。這些調用是64位安全的。

- (void)setBundleBitOfFile:(NSString*)path toBool:(BOOL)newValue 
{ 
    const char* pathFSR = [path fileSystemRepresentation]; 
    FSRef ref; 
    OSStatus err  = FSPathMakeRef((const UInt8*)pathFSR, &ref, /*isDirectory*/ NULL); 

    if (err == noErr) 
    { 
     struct FSCatalogInfo catInfo; 
     union FinderInfoTransmuter finderInfoPointers = { .bytes = catInfo.finderInfo }; 

     err = FSGetCatalogInfo(&ref, 
           kFSCatInfoFinderInfo, 
           &catInfo, 
           /*outName*/ NULL, 
           /*FSSpec*/ NULL, 
           /*parentRef*/ NULL); 

     if (err == noErr) 
     { 
      if (newValue) 
       finderInfoPointers.finderInfo->finderFlags |= kHasBundle; 
      else 
       finderInfoPointers.finderInfo->finderFlags &= ~kHasBundle; 

      FSSetCatalogInfo(&ref, 
          kFSCatInfoFinderInfo, 
          &catInfo); 
     } 
    } 
} 

- (BOOL)bundleBitOfFile:(NSString*)path 
{ 
    BOOL value   = NO; 

    const char* pathFSR = [path fileSystemRepresentation]; 
    FSRef ref; 
    OSStatus err  = FSPathMakeRef((const UInt8*)pathFSR, &ref, /*isDirectory*/ NULL); 

    if (err == noErr) 
    { 
     struct FSCatalogInfo catInfo; 
     union FinderInfoTransmuter finderInfoPointers = { .bytes = catInfo.finderInfo }; 

     err = FSGetCatalogInfo(&ref, 
           kFSCatInfoFinderInfo, 
           &catInfo, 
           /*outName*/ NULL, 
           /*FSSpec*/ NULL, 
           /*parentRef*/ NULL); 

     if (err == noErr) 
     { 
      value = (BOOL)(((finderInfoPointers.finderInfo->finderFlags) & kHasBundle) == kHasBundle); 
     } 
    } 

    return value; 
} 
4

你想做什麼?

幾乎所有的Mac OS X捆綁包實際上都是文件夾內有非常特定的文件/文件夾佈局的文件夾。很少有捆綁只是一個文件 - 它們確實存在,但並不經常。

在大多數情況下,文件上的Bundle位是完全不相關的。

i want to set a folder as a bundle. so through the file system it will looks like a file. later a want to open that package with my application. the question is how to set the folder bundle attribute through cocoa.

你真的不需要設置捆綁位。只要你的文件夾有一個擴展名(它應該),並且你的應用程序被正確配置爲打開該擴展名的文件/文件夾,它就應該在查找器中播種,就像它是一個文件一樣。

實施例:

  1. 轉到在搜索

  2. 在終端〜/桌面,做到:

  3. cd ~/Desktop

  4. mkdir aafoo

  5. mv aafoo aafoo.rtfd

(4)後,你會看到 'aafoo' 在Finder中顯示爲一個文件夾。

AFter(5),'aafoo'將改爲TextEdit文檔。沒有必要的屬性位。


好 - 夠公平的。你真的很想設置這一點。

鑑於您有命令行來執行此操作,我建議您只需使用NSTask。這可能比使用API​​來做到這一點更容易。

+0

我想將一個文件夾設置爲一個包。所以通過文件系統它看起來像一個文件。後來想要用我的應用程序打開該包。 問題是如何通過可可設置文件夾束屬性。 – Remizorrr 2010-01-05 23:32:49

+0

是的。但我不需要rtfd擴展。但例如fbp。 我需要編程。 要使任何文件夾成爲一個包,您可以通過終端使用命令: SetFile -a B文件夾名稱。 SetFile的手冊頁說 - -a屬性設置文件屬性位,其中......(等) B | b有捆綁。 問題是如何通過可可來編程。 – Remizorrr 2010-01-05 23:59:16

4

您不設置束屬性。您可以在應用程序的「信息」中定義文檔類型。plist文件,這是您指定文檔是包還是純文件的位置。

您可以在Xcode中通過選擇應用程序目標上的「Get Info」來完成此操作;切換到「屬性」選項卡,然後在「文檔類型」下添加文檔,給它一個名稱,設置其文件擴展名,選擇一個圖標文件,然後檢查「包」複選標記。

+0

當然,爲了這個工作,您需要確保您的文檔使用包結構保存,如下所示:http://developer.apple.com/mac/library/documentation/CoreFoundation/Conceptual/CFBundles/DocumentPackages/DocumentPackages .html#// apple_ref/doc/uid/10000123i-CH106-SW1 – 2010-01-06 03:52:49

+0

沒有竅門。引用文章:「你所要做的就是創建一個具有適當文件擴展名的目錄。」 – Darren 2010-01-06 04:01:28

+0

是的,你說得對。 – 2010-01-06 07:56:48

2

謝謝大家的幫助。我使用了info.plist文件的解決方案。 這是plist文件的一部分。

<key>CFBundleDocumentTypes</key> 
<array> 
    <dict> 
     <key>CFBundleTypeName</key> 
     <string>Project</string> 
     <key>LSHandlerRank</key> 
     <string>Default</string> 
     <key>LSTypeIsPackage</key> 
     <true/> 
     <key>CFBundleTypeExtensions</key> 
     <array> 
      <string>fbp</string> 
     </array> 
     <key>CFBundleTypeIconFile</key> 
     <string>document</string> 
     <key>CFBundleTypeRole</key> 
     <string>Editor</string> 
    </dict> 
</array>