• Stars
    star
    4,346
  • Rank 9,466 (Top 0.2 %)
  • Language
    Go
  • License
    MIT License
  • Created over 3 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Native Mac APIs for Go. Soon to be renamed DarwinKit!

MacDriver Logo

Native Apple APIs for Golang!

GoDoc Go Report Card @progrium on Twitter Project Forum Sponsor Project

Important
Aug 23, 2023: MacDriver is becoming DarwinKit with the 0.5.0-preview release now available! The legacy branch and previous releases are still available for existing code to work against. Help me reach the 0.5.0 release milestone or request examples you'd like to see by posting in Discussions.


DarwinKit lets you work with supported Apple frameworks and build native applications using Go. With XCode and Go 1.18+ installed, you can write this program in a main.go file:

package main

import (
	"github.com/progrium/macdriver/objc"
	"github.com/progrium/macdriver/macos"
	"github.com/progrium/macdriver/macos/appkit"
	"github.com/progrium/macdriver/macos/foundation"
	"github.com/progrium/macdriver/macos/webkit"
)

func main() {
	// runs macOS application event loop with a callback on success
	macos.RunApp(func(app appkit.Application, delegate *appkit.ApplicationDelegate) {
		app.SetActivationPolicy(appkit.ApplicationActivationPolicyRegular)
		app.ActivateIgnoringOtherApps(true)

		url := foundation.URL_URLWithString("http://progrium.com")
		req := foundation.NewURLRequestWithURL(url)
		frame := foundation.Rect{Size: foundation.Size{1440, 900}}

		config := webkit.NewWebViewConfiguration()
		wv := webkit.NewWebViewWithFrameConfiguration(frame, config)
		wv.LoadRequest(req)

		w := appkit.NewWindowWithContentRectStyleMaskBackingDefer(frame,
			appkit.ClosableWindowMask|appkit.TitledWindowMask,
			appkit.BackingStoreBuffered, false)
		objc.Retain(&w)
		w.SetContentView(wv)
		w.MakeKeyAndOrderFront(w)
		w.Center()

		delegate.SetApplicationShouldTerminateAfterLastWindowClosed(func(appkit.Application) bool {
			return true
		})
	})
}

Then in this directory run:

go mod init helloworld
go get github.com/progrium/macdriver@main
go run main.go

This may take a moment the first time, but once the window pops up you just made a macOS program without using XCode or Objective-C. Run go build to get an executable.

Although currently outside the scope of this project, if you wanted you could put this executable into an Application bundle. You could even add entitlements, then sign and notarize this bundle or executable to let others run it. It could theoretically even be put on the App Store. It could theoretically be put on an iOS, tvOS, or watchOS device, though you would have to use different platform specific frameworks.

Caveats

  • You still need to know or learn how Apple frameworks work, so you'll have to use Apple documentation and understand how to translate Objective-C example code to the equivalent Go with DarwinKit.
  • Your programs link against the actual Apple frameworks using cgo, so XCode needs to be installed for the framework headers.
  • You will be using two memory management systems. Framework objects are managed by Objective-C memory management, so be sure to read our docs on memory management with DarwinKit.
  • Exceptions in frameworks will segfault, giving you both an Objective-C stacktrace and a Go panic stacktrace. You will be debugging a hybrid Go and Objective-C program.
  • Goroutines that interact with GUI objects need to dispatch operations on the main thread otherwise it will segfault.

This is all tenable for simple programs, but these are the reasons we don't recommend large/complex programs using DarwinKit.

Examples

largetype

A Contacts/Quicksilver-style Large Type utility in under 80 lines:

largetype screenshot

pomodoro

A menu bar pomodoro timer in under 80 lines:

pomodoro gif

webshot

A webview PNG capture example in under 100 lines:

webshot screenshot

See all examples

How it works

Brief background on Objective-C

Ever since acquiring NeXT Computer in the 90s, Apple has used NeXTSTEP as the basis of their software stack, which is written in Objective-C. Unlike most systems languages with object orientation, Objective-C implements OOP as a runtime library. In fact, Objective-C is just C with the weird OOP specific syntax rewritten into C calls to libobjc, which is a normal C library implementing an object runtime. This runtime could be used to bring OOP to any language that can make calls to C code. It also lets you interact with objects and classes registered by other libraries, such as the Apple frameworks.

At the heart of DarwinKit is a package wrapping the Objective-C runtime using cgo and libffi. This is actually all you need to interact with Objective-C objects and classes, it'll just look like this:

app := objc.Call[objc.Object](objc.GetClass("NSApplication"), objc.Sel("sharedApplication"))
objc.Call[objc.Void](app, objc.Sel("run"))

So we wrap these calls in a Go API that lets us write code like this:

app := appkit.Application_SharedApplication()
app.Run()

These bindings are great, but we need to define them for every API we want to use. Presently, Apple has around 200 frameworks of nearly 5000 classes with 77k combined methods and properties. Not to mention all the constants, functions, structs, unions, and enums we need to work with those objects.

So DarwinKit generates its bindings. This is the hard part. Making sure the generation pipeline accurately produces usable bindings for all possible symbols is quite an arduous, iterative, manual process. Then since we're moving symbols that lived in a single namespace into Go packages, we have to manually decouple dependencies between them enough to avoid circular imports. If you want to help add frameworks, read our documentation on generation.

Objects are passed around as typed pointer values in Objective-C. When we receive an object from a method call in Go, the objc package receives it as a raw pointer, which it first puts into an unsafe.Pointer. The bindings for a class define a struct type that embeds an objc.Object struct, which contains a single field to hold the unsafe.Pointer. So unless working with a primitive type, you're working with an unsafe.Pointer wrapped in an objc.Object wrapped in a struct type that has the methods for the class of the object of the pointer. Be sure to read our documentation on memory management.

If you have questions, feel free to ask in the discussion forums.

Thanks

This project was inspired by and originally based on packages written by Mikkel Krautz. The latest version is based on packages written by Dong Liu.

Notice

This project is not affiliated or supported by Apple.

License

MIT

More Repositories

1

localtunnel

Expose localhost servers to the Internet
Go
3,180
star
2

bashstyle

Let's do Bash right!
1,791
star
3

gitreceive

Easily accept and handle arbitrary git pushes
Shell
1,141
star
4

buildstep

Buildstep uses Docker and Buildpacks to build applications like Heroku
Groovy
907
star
5

entrykit

Entrypoint tools for elegant, programmable containers
Go
442
star
6

keychain.io

Python
395
star
7

busybox

Busybox container with glibc+opkg
Shell
384
star
8

duplex

Full duplex modern RPC
Python
379
star
9

go-basher

Library for writing hybrid Go and Bash programs
Go
370
star
10

topframe

Local webpage screen overlay for customizing your computing experience
JavaScript
349
star
11

go-extpoints

Make Go packages extensible
Go
330
star
12

ginkgo

Python service microframework
Python
323
star
13

envy

Lightweight dev environments with a twist
JavaScript
321
star
14

termshare

Quick and easy terminal sharing.
Go
320
star
15

go-shell

Go
311
star
16

skypipe

A magic pipe in the sky for the command line
Python
307
star
17

nullmq

ZeroMQ-like sockets in the browser. Used for building gateways and generally applying ZeroMQ philosophy to browser messaging.
JavaScript
276
star
18

wssh

wssh ("wish") is a command-line utility/shell for WebSocket inspired by netcat
Python
260
star
19

viewdocs

Read the Docs meets Gist.io for simple Markdown project documentation
Go
257
star
20

qmux

wire protocol for multiplexing connections or streams into a single connection, based on a subset of the SSH Connection Protocol
Go
224
star
21

docker-stress

Docker container for generating workload stress
Dockerfile
221
star
22

nginx-appliance

A programmable Nginx container
Shell
199
star
23

pluginhook

Simple dispatcher and protocol for shell-based plugins, an improvement to hook scripts
Go
180
star
24

hookah

Asynchronous HTTP request dispatcher for webhooks
Python
144
star
25

cedarish

Heroku Cedar-ish Base Image for Docker
Shell
116
star
26

gh-release

DEPRECATED -- Utility for automating Github releases with file uploads
Shell
112
star
27

notify-io

Open notification platform for the web
Python
107
star
28

postbin

Webhook data inspector
Python
106
star
29

docker-plugins

Plugins for Docker
Shell
102
star
30

basht

Minimalist Bash test runner
Go
98
star
31

embassy

Easy, distributed discovery and routing mesh for Docker powered by Consul
Shell
94
star
32

configurator

Go
89
star
33

scriptlets

Web scripting in the cloud
JavaScript
64
star
34

raiden

Python
60
star
35

hotweb

Live reloading and ES6 hot module replacement for plain old JavaScript
Go
56
star
36

http-subscriptions

54
star
37

rootbuilder

Base Docker image for using buildroot to produce a rootfs.tar
Makefile
53
star
38

miyamoto

Python
45
star
39

oauth2-appengine

Reference server implementation for OAuth2 that runs on App Engine
Python
44
star
40

buildpack-nginx

nginx buildpack
Shell
42
star
41

DarkForest

C#
41
star
42

systembits

Simplest profiler ever. Like ohai but just shell scripts.
Shell
40
star
43

qtalk-go

versatile stream IO and RPC based IPC stack for Go
Go
40
star
44

DrEval

JavaScript sandbox (eval) as a service
Python
39
star
45

pydoozer

Python client for Doozer using gevent
Python
37
star
46

ginkgotutorial

Python
37
star
47

skywatch

Magic cloud alerting system in a self-contained command-line utility
Ruby
37
star
48

yapper

A Jabber/XMPP interface to Growl
Python
34
star
49

wolverine

Previously Miyamoto, a Twisted hub implementation of PubSubHubbub
Python
33
star
50

dockerhook

Docker event stream listener that triggers a hook script
Go
33
star
51

protocol-droid

Universal (read: HTTP) protocol bridge
Python
28
star
52

wsio

Pipe data anywhere
Ruby
22
star
53

hostpool

A worker pool manager for DigitalOcean hosts.
Go
22
star
54

clon-spec

Command-Line Object Notation: Ergonomic JSON-compatible input syntax for CLI tools.
22
star
55

shelldriver

Go
19
star
56

prototypes

Collection of experiments and prototypes
Go
19
star
57

macschema

Toolchain for generating JSON definitions of Apple APIs
Go
18
star
58

vizgo

a visual golang software editor
JavaScript
17
star
59

gh-pages-auth

Set up GitHub Pages and Auth0 authentication with minimal effort
HTML
15
star
60

consul-access

Nginx
14
star
61

mailhooks

Get Email as HTTP POST
Python
13
star
62

busybox-docker

Minimal Docker image with the Docker binary
Shell
13
star
63

tview-ssh

Example using tcell+tview over SSH using gliderlabs/ssh
Go
13
star
64

webdns

DNS over HTTP. Serve DNS with a REST API
12
star
65

irc-for-gmail

Embeddable IRC client for Gmail via Chrome extension. EXPERIMENTAL
JavaScript
11
star
66

go-plugins-lua

Lua runtime for go-plugins
Go
11
star
67

ohai-there

Easy system profiling in a Docker container
Go
11
star
68

dockerbuilder

Shell
10
star
69

javascriptd

Node.js powered script execution container
JavaScript
10
star
70

httpmail

A REST/Atom gateway to IMAP
10
star
71

docker-releasetag

Shell
10
star
72

go-streamkit

High level stream plumbing API in Go
Go
9
star
73

sveltish

Go
9
star
74

progrium.com

My website
HTML
9
star
75

gh-download

Proxy to latest Github Release asset download
Go
9
star
76

go-scripting

Go
9
star
77

tracker-widget

Pivotal Tracker widget for listing stories
Python
9
star
78

cometcatchr

An opinionated Comet client in Flash for Javascript
JavaScript
9
star
79

hackerdojo-signin

Python
8
star
80

pubsubhubbub-testsuite

Hub validation of the PubSubHubbub spec
Ruby
8
star
81

hd-events

This repo is no longer canonical! See link below:
Python
8
star
82

simplex

Go
8
star
83

groknet

ngrok as a net.Listener
Go
8
star
84

growl

A mirror of Growl from Mercurial
Objective-C
8
star
85

docker-9p

Docker Volume Plugin for 9P
Go
8
star
86

websocket-radio

JavaScript
8
star
87

dockerhub-tag

Go
7
star
88

registrator

I hate Docker Hub
7
star
89

docker-plugin

docker plugin subcommand UX prototype
Shell
7
star
90

domfo

Simple domain forwarder -- redirects web requests based on URL in TXT record
Python
7
star
91

electron-largetype

Large Type for Electron apps
HTML
7
star
92

jabberhooks

Jabber to webhook service
Python
6
star
93

platformer

Go
6
star
94

webhooks

Website for webhooks.org
HTML
6
star
95

hackerdojo-signup

The Hacker Dojo member signup app
Python
6
star
96

hackerdojo-kiosk

JavaScript
6
star
97

domdori

Domains Done Right
Python
6
star
98

goja-automerge

Automerge.js in Go via goja
JavaScript
6
star
99

stomp4py

Python
6
star
100

usb

universal seinfeld binary
Go
6
star