1

I'm trying to read comment field from file. enter image description here

Found this thread about how to do it, but can't find MDItem corresponding class in Xamarin.

Any ideas where to find it or what to use instead?

arsena
  • 1,935
  • 19
  • 36

1 Answers1

2

Some of the CoreServices are not wrapped within the current Xamarin.Mac (Version: 2.10.0.57).

Most of them pass CFxxxx-based references around, so they are easy to wrap and implement via some Interop calls.

Finder-based File comment

enter image description here

Code Example:

var fileURL = NSUrl.FromString("/Users/sushi/Desktop/DFeedback_FeedBack.png");
var mMDItemRef = MDItemCreateWithURL(IntPtr.Zero, fileURL.Handle);

var mCFTypeRef = MDItemCopyAttribute(mMDItemRef, new CFString("kMDItemFinderComment").Handle);
var finderComment = NSString.FromHandle(mCFTypeRef);
Console.WriteLine(finderComment);

Note: The interop calls should be tested for null in production code, see return signature comments in header file.

Application Output:

StackOverflow

Interop setup:

// @function MDItemCreateWithURL
// Returns an metadata item for the given path.
// @param allocator The CFAllocator which should be used to allocate
// memory for the query and its sub-storage.This
// parameter may be NULL in which case the current default
// CFAllocator is used.
// @param url A url to the file for which to create the MDItem.
// [[Currently, the file must exist.MDItemRefs may or
// may not be uniqued.Use CFEqual() to compare them.]]
// @result An MDItemRef, or NULL on failure.

//MD_EXPORT MDItemRef MDItemCreateWithURL(CFAllocatorRef allocator, CFURLRef url) AVAILABLE_MAC_OS_X_VERSION_10_6_AND_LATER;
[DllImport(Constants.CoreServicesLibrary)]
extern static /* MDItemRef */ IntPtr MDItemCreateWithURL(/* CFAllocatorRef __nullable */ IntPtr allocator, /* CFURLRef */ IntPtr inURL);

//@function MDItemCopyAttribute
//Returns the value of the given attribute for the item.
//@param item The item to be interrogated.
//@param name The name of the desired attribute.
//@result A CFTypeRef, or NULL on failure, or if the attribute
//does not exist, of if the attribute is not readable.

//MD_EXPORT CFTypeRef MDItemCopyAttribute(MDItemRef item, CFStringRef name) MD_AVAIL;
[DllImport(Constants.CoreServicesLibrary)]
extern static /* CFTypeRef */ IntPtr MDItemCopyAttribute(/* MDItemRef */ IntPtr item, /* CFStringRef */ IntPtr name);

Xcode Obj-C Header Reference:

MDItem.h

Local Xcode Ref:: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/Metadata.framework/Versions/A/Headers

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • Thanks for the answer, I'll test it out soon and get back with the result. – arsena Aug 16 '16 at 06:53
  • I have tested it too, here is the only problem, sometimes it return old comment, maybe it cached values or somehow. I have moved item + changed comment while moving -> put back and get comment , the old comment was returned. any idea? – Nininea Dec 27 '16 at 10:32
  • @Nininea I have never seen an old comment return. If the comment is saved/commit via Finder or programmatically then calling `MDItemCreateWithURL` and then `MDItemCopyAttribute` should always return the "new" comment. Make sure that you are calling `MDItemCreateWithURL` each time and reusing an existing reference. – SushiHangover Dec 27 '16 at 20:57
  • @SushiHangover I still have the problem, I described above. I think that "mCFTypeRef" is not release , and somehow uses cashed value :/ I found the native example : https://github.com/danielpunkass/iMediaLegacy/blob/master/NSURL%2BiMedia.m – Nininea Feb 12 '17 at 12:32
  • @SushiHangover can you check please following link : https://github.com/xamarin/mac-samples – Nininea Feb 23 '17 at 12:58
  • Not sure what you mean? – SushiHangover Feb 23 '17 at 13:01
  • @SushiHangover this demo project show the bug I was talking about – Nininea Feb 23 '17 at 13:03
  • sorry, it was wrong link . here it is : https://github.com/Nininea/Xamarin.mac-File-Comment- – Nininea Feb 23 '17 at 13:15