A little information :
Cocoa is object oriented and works by sending a message to a receiver.
The Objective-C language uses square brackets for this. For example to print a view it uses
[myView print:sender];
PureBasic doesn't support square brackets like this so a different approach is required to use this message sending system.
CocoaMessage is what you need in this case.
Syntax :
CocoaMessage(@ReturnValue, Object, Method.s [,...])
When 0 is passed for the return value address, the result is returned from the procedure itself if it fits in the PB Integer type.
When 0 is passed for object, a class name can be passed together with the first part of the method separated by a space character.
When non integer values have to be passed, they can be passed by reference by adding a @ after a colon character.
You can also pass the address of a PureBasic string using $ when a NSString is expected. CocoaMessage does the conversion for you internally.
Example :
Code: Select all
MyTransform = CocoaMessage(0, 0, "NSAffineTransform transform"); get an identity transform
sx.CGFloat = 5.5
sy.CGFloat = 20
CocoaMessage(0, MyTransForm, "scaleXBy:@", @sx, "yBy:@", @sy); scale x by 5.5, y by 20
MyTransformStruct.NSAffineTransform
CocoaMessage(@MyTransformStruct, MyTransForm, "transformStruct"); get the transform structure
Debug MyTransformStruct\m11; debug outputs 5.5
http://developer.apple.com/library/mac/ ... index.html
When going through the examples in this thread, it sometimes can happen that constants which are defined in a source were added to
PureBasic itself in a later version. If that happens you can simply remove the constant declaration from the example code.
Methods, Tips & Tricks
- AddressBook - Accessing the OS X address book
Alert - Display an alert (MessageRequester with OK button) specifying a predefined icon
Alert - Display an alert (NSAlert) with a suppression checkbox
AppleScript
Application - Change the application icon while running
Application - Get file names passed to application
Application - Set badge label
Blocks - Working with blocks (NSBlock)
ButtonGadget - Bezel styles
ButtonGadget - Recolor a button gadget using a Core Image Filter
ButtonGadget - Set a key equivalent for a button
ButtonGadget - Set as default button
Camera - iSight snapshot
CFNotificationCenter - Working with CFNotificationCenter (notifications between apps)
ContainerGadget - Set a background image
Cursor - Custom cursor using a PureBasic image
Cursor - Display all available system cursors
Disk - Display information about mounted volumes
Disk - Get free disk space
EditorGadget - Enable undo
EditorGadget - Insert text at cursor position
EditorGadget - Load a rtf file
EditorGadget - Save to a file in a specific file format like rtf or html
EditorGadget - Scroll to end of text
EditorGadget - Select text area
EditorGadget - Set html formatted text
EditorGadget - Set text color of specific areas
EditorGadget - Wrap words
Events - Handling keyboard and mouse events
Events - Tapping keyboard and mouse events using CGEvent
File - Get all file names from a directory including its subdirectories
File - Get an icon image for a specific file type
File Viewer - Activate file viewer with multiple files
FileRequester - FileRequester (Open / Save)
Font - List all available fonts
Font - Some functions for getting name and size for PB's gadget fonts
Gadget - Find out the what class is behind a gadget
Gadget - Print
GKRandomDistribution - Random numbers example (OS X 10.11+)
HID - List all Human Interface Devices (HID) currently attached
HyperLinkGadget - Change cursor hovering over HyperLinkGadget
Image - Anti-aliased drawing using NSColor, NSGradient and NSBezierPath
Image - Apply a core image filter
Image - Capture the main display into an image
Image - Display icon of the current user's Mac
Image - Draw a polygon
Image - Draw three or nine part image
Image - List all predefined NSImageName icons and their minimum OS X version
Image - Load / catch image (all OS X supported image types) & save image
Image - Set image size (useful for working with retina images)
Image - Write png encoded image data into a file
InputRequester - InputRequester with cancel button and ability to set default text
Keyboard - Check modifier keys
Keyboard - Global keyboard tap
Key-Value Observer example
ListIconGadget - Change height of rows (works also with ListViewGadget)
ListIconGadget - Colorize single cells
ListIconGadget - Detect left click on column header and row
ListIconGadget - Disable selection highlighting
ListIconGadget - Editable cells
ListIconGadget - Get the number of currently visible rows
ListIconGadget - Hide title line
ListIconGadget - Programmatically scroll row or column into visible part
ListIconGadget - Select entire column by clicking column header
ListIconGadget - Select item using right-click
ListIconGadget - Select item using right-click (simplified solution)
ListIconGadget - Set a ToolTip for a column header
ListIconGadget - Set column title
ListIconGadget - Set the alignment of text in columns
ListIconGadget - Sorting data by clicking a column header
ListIconGadget - Toggle grid lines
ListIconGadget - Transparent ListIconGadget
ListViewGadget - Alternating row colors
ListViewGadget - Colorize single rows
ListViewGadget - Enable / disable scrollers
ListViewGadget - Find all selected items
Local information - Get language code, decimal separator and seconds from GMT
Local information - Get language information
Menu - Adding a key equivalent to a menu item
MessageRequester - A message requester like example with multiple buttons
Mouse - Get object under mouse
NSDecimal(Number) - Working with decimal numbers
NSDrawer - Creating and using a NSDrawer object
NSPredicate - Filtering using NSPredicate
NSStatusBar - Example of working with the system-wide status bar located in the menu bar
NSUserDefaults - Detect if 'dark mode' is active
NSUserDefaults - Get default user language/locale settings
NSUserDefaults - Working with NSUserDefaults instead of a Preference file
Operating System - Get operatingSystemVersionString
Operating System - OSX Version check
PanelGadget - Change color theme of selected tab (pre OS X 10.7)
PanelGadget - Change location of tabs
PDF - Simple PDF viewer
Printer - Get current page settings
Printer - List all available printers
Printer - List all available printers with supported paper sizes
Process - Show a list of processes with time launched
ProgressBarGadget - Spinning indicator
ProgressBarGadget - Toggle animation (pre OS X 10.7)
Restart application
Scanner - Using an OS X supported scanner
Sheet - Modal sheet with completion handler
Sound - Working with Cocoa sound objects
StringGadget - Detect right click
StringGadget - Set cursor position
StringGadget - Set required StringGadget height
StringGadget - Set text alignment to align right
SysTray - Adding a menu to a status bar item
Target / action example
TextGadget - Set text color of specific areas
Time Zone - Get time zone information (name, offset, DST)
ToolBar - Change several properties of a ToolBar
ToolBar - Icon positions
ToolBar - Using a search field inside a ToolBar
ToolBar - Using a segmented control inside a ToolBar
TrackBarGadget - Turn TrackBarGadget into a fixed size circular slider
TreeGadget - Change frame around cells
TreeGadget - Change indentation of subitems
TreeGadget - Subnodes with indented checkboxes
TreeGadget - TreeGadget icons
UUID - Create UUID
WebGadget - Get parts of html (head, body, title)
WebGadget - Navigation callback alternative
WebGadget - Print content
WebGadget - Search for a text on the page
Window - Check if a window is visible
Window - Enable fullscreen button (OS X 10.7+)
Window - Fade the window
Window - FullScreen (several procedures)
Window - FullScreen on OS X 10.6
Window - Hide minimize and maximize buttons
Window - Live resize using NSWindowDidResizeNotification
Window - Make use of a second screen
Window - Move window animated
Window - Print
Window - Semi transparent window
Window - Set black dot in close button (document edited)
Window - Simulate a custom shaped window
Window - Toggle attributes, shadow and toolbar button
Window - Toggle fullscreen by software
Window - Use a window as a sheet for another window
Workspace - Set desktop wallpaper