The following example returns the Finder and other metadata for a given file.
PLEASE NOTE: The Associate server will strip backslash characters
from this code. They are used in formatting methods and functions for
tabs and returns. I figure anyone interested in this code, would be
able to fix this without much problem.
To get the returned results in human-readable form, I had to resort
to some Cocoa methods that don't appear to have any CF equivalents.
I have remarked out my failed experiment with
MDSchemaCopyDisplayNameForAttribute which I could not get to work.
Maybe someone will chime in and point out the error of my ways. My
headers indicate it is available in OS X 10.4 and newer. If it
worked, I would expected it to return attribute names in a
Finder-like format in the current locale, rather than in the raw
CFString form returned by this code.
Ken
include "ConsoleWindow"
#if ndef _DEFINEDINCARBON
#define MDItemRef as pointer // to struct MDItem
toolbox fn MDItemCreate ( CFAllocatorRef allocator, CFStringRef path
) = MDItemRef
toolbox fn MDItemCopyAttributeNames ( MDItemRef item ) = CFArrayRef
toolbox fn MDItemCopyAttributes ( MDItemRef item, CFArrayRef names )
= CFDictionaryRef
toolbox fn MDSchemaCopyDisplayNameForAttribute ( CFStringRef name ) =
CFStringRef
#endif
BeginCDeclaration
CFStringRef GetAttributeNamesAsString( CFArrayRef
inspectedRefAttributeNames, int row ){
CFStringRef attributeName = NULL;
// CFStringRef valueStr, outStr = NULL;
attributeName = (CFStringRef)CFArrayGetValueAtIndex(
inspectedRefAttributeNames, row );
/*
valueStr = MDSchemaCopyDisplayNameForAttribute( attributeName );
if ( CFStringGetLength( valueStr ) ){
outStr = CFStringCreateCopy( kCFAllocatorDefault, valueStr );
CFRelease( valueStr );
}else{
outStr = CFStringCreateCopy( kCFAllocatorDefault, attributeName );
}
*/
return ( attributeName );
}
CFMutableStringRef GetAttributeValuesAsString( CFDictionaryRef
inspectedRefAttributeValues, CFStringRef attributeName, int row ){
NSDictionary *valueDict;
CFMutableStringRef outStr = NULL;
NSMutableString *formattedValue;
outStr = CFStringCreateMutable( kCFAllocatorDefault, 0 );
valueDict = CFDictionaryGetValue( inspectedRefAttributeValues,
attributeName );
if ([valueDict respondsToSelector:
@selector(descriptionInStringsFileFormat)]){
formattedValue = [[valueDict descriptionInStringsFileFormat]
mutableCopy];
}else{
formattedValue = [[valueDict description] mutableCopy];
}
// Format for printing
[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])];
[formattedValue replaceOccurrencesOfString:@"("
withString:@"" options:0 range:NSMakeRange(0,[formattedValue
length])];
[formattedValue replaceOccurrencesOfString:@")"
withString:@"" options:0 range:NSMakeRange(0,[formattedValue
length])];
[formattedValue replaceOccurrencesOfString:@", "
withString:@",\r\t" options:0 range:NSMakeRange(0,[formattedValue
length])];
CFStringAppend( outStr, ( CFMutableStringRef )formattedValue );
CFStringTrimWhitespace( outStr );
[formattedValue autorelease];
return ( outStr );
}
EndC
toolbox fn GetAttributeNamesAsString( CFArrayRef, int ) = CFStringRef
toolbox fn GetAttributeValuesAsString( CFDictionaryRef, CFStringRef,
int ) = CFMutableStringRef
dim as Str255 fileName
dim as CFURLRef url
dim as MDItemRef inspectedRef
dim as CFArrayRef fileArray
dim as CFDictionaryRef dictValues
dim as CFMutableStringRef infoMutStr, nameAttributesStr, valueAttributesStr
dim as CFStringRef path, attributeName
dim as CFIndex i, row, result
dim as CFTypeRef value
// Create CFString to hold all information
infoMutStr = fn CFStringCreateMutable( _kCFAllocatorDefault, 0 )
fileName = files$( _CFURLRefOpen,,, url )
long if ( fileName[0] )
path = fn CFURLCopyFileSystemPath( url, _kCFURLPOSIXPathStyle )
long if ( path )
inspectedRef = fn MDItemCreate ( _kCFAllocatorDefault, path )
long if ( inspectedRef )
fileArray = fn MDItemCopyAttributeNames( inspectedRef )
dictValues = fn MDItemCopyAttributes( inspectedRef, fileArray )
end if
CFRelease( path )
end if
xelse
// User canceled
end if
for i = 0 to fn CFArrayGetCount( fileArray ) -1
nameAttributesStr = fn GetAttributeNamesAsString( fileArray, i )
valueAttributesStr = fn GetAttributeValuesAsString( dictValues,
nameAttributesStr, i )
CFStringAppend( infoMutStr, nameAttributesStr )
CFRelease( nameAttributesStr )
CFStringAppend( infoMutStr, @":\r\t" )
CFStringAppend( infoMutStr, valueAttributesStr )
CFRelease( valueAttributesStr )
CFStringAppend( infoMutStr, @"\r\r" )
next
fn HIViewSetText( sConsoleHITextView, infoMutStr )
CFRelease( infoMutStr )