DotMac

プログラムに関する覚え書き等

Changing documents' creator/クリエータを変更 

以下アップルのサイトより From Apple's website



- (NSDictionary *)fileAttributesToWriteToFile:(NSString *)fullDocumentPath

  ofType:(NSString *)documentTypeName

saveOperation:(NSSaveOperationType)saveOperationType

{

    NSDictionary *infoPlist = [[NSBundle mainBundle] infoDictionary];

    NSString *creatorCodeString;

    NSArray *documentTypes;

    NSNumber *typeCode, *creatorCode;

    NSMutableDictionary *newAttributes;

    

    typeCode = creatorCode = nil;

    

    // creatorCode を(存在すれば)アプリケーションの HFS クリエータコードに

    // 設定する。

    creatorCodeString = [infoPlist objectForKey:@"CFBundleSignature"];

    if(creatorCodeString)

    {

        creatorCode = [NSNumber

            numberWithUnsignedLong:NSHFSTypeCodeFromFileType([NSString

            stringWithFormat:@"'%@'",creatorCodeString])];

    }

    

    // 続いて、このタイプに対応する Info.plist ディクショナリエントリを探す。

    // 関連付けられている最初の HFS タイプコードを(もしあれば)使用する。

    documentTypes = [infoPlist objectForKey:@"CFBundleDocumentTypes"];

    if(documentTypes)

    {

        int i, count = [documentTypes count];

        

        for(i = 0; i < count; i++)

        {

            NSString *type = [[documentTypes objectAtIndex:i]

                objectForKey:@"CFBundleTypeName"];

            if(type && [type isEqualToString:documentTypeName])

            {

                NSArray *typeCodeStrings = [[documentTypes objectAtIndex:i]

                    objectForKey:@"CFBundleTypeOSTypes"];

                if(typeCodeStrings)

                {

                    NSString *firstTypeCodeString = [typeCodeStrings

                        objectAtIndex:0];

                    if (firstTypeCodeString)

                    {

                        typeCode = [NSNumber                            numberWithUnsignedLong:

NSHFSTypeCodeFromFileType([NSString

                           stringWithFormat:@"'%@'",firstTypeCodeString])];

                    }

                }

                break;

            }

        }

    }

    // タイプとクリエータコードのどちらも存在しなければ、デフォルトの実装を使用する。

    if(!(typeCode || creatorCode))

    {

        return [super fileAttributesToWriteToFile:fullDocumentPath

  ofType:documentTypeName saveOperation:saveOperationType];

    }

    

    // さもなければ、ディクショナリにタイプとクリエータを追加する。

    newAttributes = [NSMutableDictionary dictionaryWithDictionary:[super

        fileAttributesToWriteToFile:fullDocumentPath ofType:documentTypeName

  saveOperation:saveOperationType]];

    if(typeCode)

        [newAttributes setObject:typeCode forKey:NSFileHFSTypeCode];

    if(creatorCode)

        [newAttributes setObject:creatorCode forKey:NSFileHFSCreatorCode];

    return newAttributes;

}


 

Parting Words (著作権, 連絡先情報, etc.)