CocoaMessage()

Syntax

Result = CocoaMessage(ReturnValueAddress, Object, Method$ [, ParameterValue [, Parameter$, [, ParamaterValue, ...]]])
Description
For advanced users. Available on OS X only, it allows to easily send an Objective-C message to the OS X framework and access any API. Usually Objective-C use brackets to have a clear syntax for messages. As PureBasic doesn't have built-in Objective-C support, it needs to emulate it, so the syntax is a bit different. Once learnt, it's easy to call the required API. To get more examples, please read the following thread on the forums.

Parameters

ReturnValueAddress If the API call return a structure or a type different than 'integer', this field is used to set the returning result. An address to the structure or variable needs to be specified. If zero is specified, the result will be ignored.
Object The object on which the Objective-C methods will be called. It can be zero if the method is a static method (mostly when creating an object).
Method$ The method to call on the object, usually followed by a semicolon (':'). If the method needs a structure as parameter, '@' needs to be appended after the semicolon. If the method expect a string as parameter, '$' can be appended after the semicolon, so the string will be automatically converted in a temporary NSString. This is not required, but it can be useful and ease the coding. If the method isn't supported by the object, a debugger message will be raised at runtime.
ParameterValue (optional) The parameter value associated to the previous method.
Parameter$ (optional) The next method parameter. PureBasic support up to 7 method parameters.

Return value

Integer return value. Useful for object creation id.

Remarks

PureBasic has already setup a temporary memory pool which is flushed every time WindowEvent() or WaitWindowEvent() is called. If you need to release big objects immediately, you will have to create a local memory pool around your calls.

Example: with string

  ; Objective-C: 
  ;   ColorList = [NSColorList colorListNamed:@"Crayons"];
  ;
  ColorList = CocoaMessage(0, 0, "NSColorList colorListNamed:$", @"Crayons") ; Will create an NSString for "Crayons" under the hood

Example: with complex type

  ; Objective-C: 
  ;   Transform = [NSAffineTransform transform];
  ;
  Transform = CocoaMessage(0, 0, "NSAffineTransform transform") ; Get an identity transform

  ; Objective-C: 
  ;   [TransForm scaleXBy:sx yBy:sy];
  ;
  sx.CGFloat = 5.5
  sy.CGFloat = 20
  CocoaMessage(0, TransForm, "scaleXBy:@", @sx, "yBy:@", @sy) ; Scale x by 5.5, y by 20. As sx is not an integer, '@' needs to be specified
  
  ; Objective-C: 
  ;   NSAffineTransform TransformStruct = [TransForm transformStruct];
  ;
  CocoaMessage(@TransformStruct.NSAffineTransform, TransForm, "transformStruct") ; Get the transform structure
  
  Debug TransformStruct\m11 ; debug outputs 5.5

Supported OS

MacOS X

<- CPUName() - System Index - ComputerName() ->