Page 1 of 1

[solved] (OSX) get/set cocoa property

Posted: Thu Sep 17, 2015 5:43 pm
by erion
Hello,

I am trying to retrieve/set various properties for classes, e.g. 'speaking' of NSSpeechSynthesizer.
As far as I know, CocoaMessage() allows methods to be called, but it does not support properties. Is there a way to do this in PB?

Erion

Re: (OSX) get/set cocoa property

Posted: Thu Sep 17, 2015 7:10 pm
by wilbert
erion wrote:I am trying to retrieve/set various properties for classes, e.g. 'speaking' of NSSpeechSynthesizer.
As far as I know, CocoaMessage() allows methods to be called, but it does not support properties. Is there a way to do this in PB?
A property is a combination of a getter and setter function or in case of readonly a getter function only.
If you look at the documentation for NSSpeechSynthesizer, it says
@property(getter=isSpeaking, readonly) BOOL speaking
So in this case the function you need is called isSpeaking

Code: Select all

SpeechSynthesizer = CocoaMessage(0, CocoaMessage(0, 0, "NSSpeechSynthesizer alloc"), "initWithVoice:$", @"com.apple.speech.synthesis.voice.Alex")

Debug CocoaMessage(0, SpeechSynthesizer, "isSpeaking")

CocoaMessage(0, SpeechSynthesizer, "startSpeakingString:$", @"Your Mac is speaking to you")

Delay(100)

Debug CocoaMessage(0, SpeechSynthesizer, "isSpeaking")

MessageRequester("", "Your Mac is speaking to you")

Re: (OSX) get/set cocoa property

Posted: Thu Sep 17, 2015 8:16 pm
by erion
Marvellous, thank you!
I wasn't expecting getter/setter exposure because of my Windows background, but this definitely makes things easier.
Now I wish we had native Cocoa type conversion, such as NSArray to PB array, etc. :)

Once again, huge thanks for your help!

Re: (OSX) get/set cocoa property

Posted: Fri Sep 18, 2015 5:02 am
by wilbert
erion wrote:Now I wish we had native Cocoa type conversion, such as NSArray to PB array, etc. :)
That would be very difficult.
To convert a PB string array for example, you would need to convert each item into a NSString also.
But when you convert an array of integer values, you would need a different approach.