-
-
Notifications
You must be signed in to change notification settings - Fork 160
Introduction to DarwinKit
DarwinKit is a new implementation of Go bindings for Apple's frameworks. It allows for calling Objective-c methods thru Go. DarwinKit binds far more classes than MacDriver providing a much better experience for the programmer.
- Go 1.18 or later
- Mac OS version: to be determined
- To be determined
- The cocoa package is now called appkit
- The core package is now called foundation
- New convenience API like NSWindow's NewWindowWithSize and NSButton's NewButtonWithTitle. See file appkit_custom.go for more information.
- "NS" prefix dropped from classes' and functions' name.
- As of this time DarwinKit is not complete yet
- There may be more breaking changes to the API
- NSMakeRect() is not available yet
Install DarwinKit
In the Terminal go to a folder you would like to store DarwinKit.
Then enter this command to download the source code:
Setup An Example Program
Create a new file called main.go.
Enter this code into the file:
package main
import (
"fmt"
"github.com/progrium/macdriver/helper/action"
"github.com/progrium/macdriver/macos/appkit"
"github.com/progrium/macdriver/macos/foundation"
"github.com/progrium/macdriver/objc"
)
// DarwinKit will supply this function in the future
func NSMakeRect(x, y, width, height float64) foundation.Rect {
return foundation.Rect{Origin: foundation.Point{X: x, Y: y}, Size: foundation.Size{Width: width, Height: height}}
}
func main() {
// Setup the application
app := appkit.Application_SharedApplication()
app.SetActivationPolicy(appkit.ApplicationActivationPolicyRegular)
app.ActivateIgnoringOtherApps(true)
// Setup the window
w := appkit.NewWindowWithSize(300, 100)
w.SetTitle("Example Program")
w.MakeKeyAndOrderFront(nil)
w.Center()
// Setup the button
button := appkit.NewButtonWithTitle("Push Me")
button.SetFrame(NSMakeRect(50, 50, 200, 20))
action.Set(button, doButton)
w.ContentView().AddSubview(button)
app.Run()
}
// The action method for button
func doButton(sender objc.Object) {
fmt.Println("Welcome to DarwinKit")
}
Save the file.
Run this command in the Terminal: go mod init program
Open the go.mod file and add these two lines to the end of the file:
require github.com/progrium/macdriver v0.5.0
replace github.com/progrium/macdriver => <path to your git downloaded macdriver folder>
You need to replace "path to your git downloaded macdriver folder" with your folder's actual path.
The easy way to do this is to drag your folder to the Terminal's window. This will display the full path.
Then copy and paste the full path into the go.mod file.
To run this program enter this command in the Terminal: go run main.go
You should see this window appear if all was successful:

If there are any issues with this documentation please create a new issue about it.
Last updated: 12-10-2023