madsmtm / objc2

Bindings to Apple's frameworks in Rust
https://docs.rs/objc2/
MIT License
280 stars 35 forks source link

Missing `descriptorType` at NSAppleEventDescriptor #590

Open mdevils opened 2 months ago

mdevils commented 2 months ago

Hello!

I wanted to access descriptorType at NSAppleEventDescriptor in order to understand the type of the descriptor, but couldn't find the corresponding field in the generated icrate package. Is there a way to access that field for me?

madsmtm commented 2 months ago

The reason it's not present is that the type of that field is DescType, which comes from the CoreServices framework, which is not yet mapped.

You can do the following in the meantime. Depending on your needs, you might have to pull in the core-services crate too.

use icrate::Foundation::NSAppleEventDescriptor;
use objc2::msg_send;

use core_services::DescType; // If using `core-services`
type DescType = u32; // If not using `core-services`

pub fn get_descriptor_type(desc: &NSAppleEventDescriptor) -> DescType {
    unsafe { msg_send![desc, descriptorType] }
}
mdevils commented 2 months ago

@madsmtm thank you for your help. Could you please advice me, how do I call this method?

[NSAppleEventDescriptor init(string:)]

Info: https://developer.apple.com/documentation/foundation/nsappleeventdescriptor/1415227-init

madsmtm commented 2 months ago

First, have a look at the Objective-C documentation instead: https://developer.apple.com/documentation/foundation/nsappleeventdescriptor/1415227-descriptorwithstring?language=objc

Now you can do roughly what I did above, except using msg_send_id! as this function is returning an object:

pub fn descriptor_with_string(string: &NSString) -> Id<NSAppleEventDescriptor> {
    unsafe { msg_send_id![NSAppleEventDescriptor::class(), descriptorWithString: string] }
}
mdevils commented 2 months ago

@madsmtm thank you!