忍者ブログ
MASTER →  ADMIN / NEW ENTRY / COMMENT
現代魔法(nearly equal 情報技術)を勉強中な人のメモ(チラシの裏)
/ 2024/03/29 (Fri) / 編集
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

/ 2007/12/04 (Tue) / 編集
ま、そんなわけで

やっとファイル情報の取得ができたのでやり方をメモっとく

ちなみに、inspectedRefはMDItemRef型、inspectedRefAttributeNamesはCFArrayRef型、inspectedRefAttributeValuesはCFDictionaryRef型でヘッダファイルに定義してある。
それからappendTextは文字列をTextViewに出力するための自作関数だったりするのでとくに今回の内容とは関係ない。

緑色の文字の部分を追加
- (void)getFileDatas:(NSString *)filepath {
CFStringRef attributeName;
NSMutableString *formattedValue;
id attribute;
id value;

//NSImageViewに画像を表示
[Imagewall1 setImage:[[[NSImage alloc] initWithContentsOfFile:filepath] autorelease]];

[text insertText:filepath];

//MDItemを作成
inspectedRef = MDItemCreate(kCFAllocatorDefault,(CFStringRef)filepath);
if (inspectedRef){
//属性名の配列を取得
inspectedRefAttributeNames = MDItemCopyAttributeNames(inspectedRef);
//属性の内容の配列を取得
inspectedRefAttributeValues = MDItemCopyAttributes(inspectedRef,inspectedRefAttributeNames);;
}

int i;
//配列の長さを調べる
int arraycount = CFArrayGetCount(inspectedRefAttributeNames);
for(i=0;i
//インデックスから配列の属性名を取得
attributeName = CFArrayGetValueAtIndex(inspectedRefAttributeNames,i);
//属性名を読みやすい文字に変換 ex)kMDItemKind -> Kind
attribute = (id)MDSchemaCopyDisplayNameForAttribute(attributeName);

if([(NSString *)attribute length]){
message = [NSString stringWithFormat:@"[%@] -> ",attribute];
[self appendText:message];
}else{//「読みやすい文字」が用意されてないものもある
message = [NSString stringWithFormat:@"[%@] -> ",attributeName];
[self appendText:message];
}

//属性名で指定されたinspectedRefAttributeValuesの中身を取得
value = (id)CFDictionaryGetValue(inspectedRefAttributeValues,attributeName);

//string型で返す
if ([value respondsToSelector: @selector(descriptionInStringsFileFormat)]){
formattedValue = [[value descriptionInStringsFileFormat] mutableCopy];
}else{
formattedValue = [[value description] mutableCopy];
}

/* \t,\r,\n を削除 */
[formattedValue replaceOccurrencesOfString:@"\t" withString:@"" options:0 range:NSMakeRange(0,[formattedValue length])];
[formattedValue replaceOccurrencesOfString:@"\r" withString:@"" options:0 range:NSMakeRange(0,[formattedValue length])];
[formattedValue replaceOccurrencesOfString:@"\n" withString:@"" options:0 range:NSMakeRange(0,[formattedValue length])];

if([attribute isEqualToString:@"Display name"] | [attribute isEqualToString:@"Filename"]){
//ファイル名はクリックするとfinderでそのファイルを開くようにする
NSURL *fileurl = [NSURL fileURLWithPath:filepath];
//
NSDictionary *attrs = [NSDictionary dictionaryWithObject:fileurl forKey:NSLinkAttributeName];
//Link属性付きの文字列に変換
NSAttributedString* linkname = [[NSAttributedString alloc] initWithString:formattedValue attributes:attrs];
//[self appendText:linkname];//「warning: pussing argument 1 of 'appendText' from distinct Objective-C」がでる
[text insertText:linkname];[self appendText:@"\n"];
}else{
message = [NSString stringWithFormat:@"%@ \n",formattedValue];
[self appendText:message];
}
}

}
}

うっかりわすれてたattibuteの処理を追加、どうやらMDSchemaCopyDisplayNameForAttributeで取得できない属性名もあるらしいね
あとNSTextViewに表示されるファイル名をクリックするとそのファイルをファインダーで開くようにした。

拍手[0回]

PR
忍者ブログ [PR]