Skip to content

Crash Course in MacDriver

programmingkidx edited this page Jul 4, 2023 · 2 revisions

Crash Course in MacDriver

What is it?
A translation layer between Objective-C and Go.

Why would I use it?

  • You really like the Go language
  • You want to write Mac OS software
  • You want to use the Appkit framework that Cocoa developers love

Who can use it?
Anyone from hobbiest to commercial software companies.

System Requirements?

  • Mac OS: the newer the better (I use Mac OS 12.3)
  • Go: a recent version (I use version 1.20)

Where do I find documentation?
Good question. There isn't much yet. You can take the Cocoa API and figure out how they look in MacDriver by following these suggestions:

  • Class methods: Classname_methodname()
  • Instance methods: Usually you can put the named arguments together and add an underscore. Sometimes you need to add an underscore at the end. Example: NSUserDefaults' setObject: forKey: would become SetObject_ForKey().

Quick Tutorial:
Create a new folder, call it program.
Inside the program folder create a new file called main.go.
Enter this code into the main.go file:

package main

import "github.com/progrium/macdriver/cocoa"
import "github.com/progrium/macdriver/objc"

func main() {

	app := cocoa.NSApp_WithDidLaunch(func(n objc.Object) {
		win := cocoa.NSWindow_New()
		win.SetTitle("Hello world")
		win.MakeKeyAndOrderFront(nil)
	})

	app.SetActivationPolicy(cocoa.NSApplicationActivationPolicyRegular)
	app.ActivateIgnoringOtherApps(true)
	app.Run()
}

Open the Terminal application. Type "cd" and then add a space. Drag the program folder from the Finder to the terminal window.

  • The full path of the folder should be displayed.

Push the return key to execute the command.

Run these commands in the terminal if you are using Go version 1.15 or older:

  • go get github.com/progrium/macdriver/cocoa
  • go get github.com/progrium/macdriver/objc

Run these commands in the terminal if you are using Go version 1.16 or newer:

  • go mod init program
  • go get github.com/progrium/macdriver/cocoa
  • go get github.com/progrium/macdriver/objc

Run the program by executing this command:

  • go run main.go

You should see the GUI equivalent of a "hello world" program: image

To quit this program enter control-c in the Terminal.

Congratulations! You just made your first MacDriver program.