Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for NSPasteboard #25

Merged
merged 4 commits into from
Mar 8, 2021
Merged

Conversation

flexzuu
Copy link
Contributor

@flexzuu flexzuu commented Feb 28, 2021

This PR adds support for NSPasteboard. Currently it covers the basic read from and write to pasteboard operations.

I included the objc code that i used to get the constants as comment. If anyone knows how we can access them directly instead let me know. I would gladly replace it.

Also i did not include the constants NSFileContentsPboardType, NSFindPanelSearchOptionsPboardType Pasteboard types as they had different contents that i was not sure about.

In terms of testing, i use this as showcased in the example with string only for now and have not yet tested pictures or anything else.

Also i spend some time to get the original apple doc comments converted and linked in the relevant places. Not sure if we want to do this or not.

@progrium
Copy link
Owner

progrium commented Mar 8, 2021

I love the links to relevant docs, I really wanted to do that. Hopefully others will continue or we can just make it part of the PR acceptance process.

Some other places I look for constant values, if the docs don't say, sometimes you can find out from .bridgesupport files.

@progrium progrium merged commit c64d62d into progrium:main Mar 8, 2021
@LinkinStars
Copy link

I want to read pictures from pasteboard.

gp := cocoa.NSPasteboard_GeneralPasteboard()

s := gp.Send("dataForType:",  core.NSString_FromString(cocoa.NSPasteboardTypePNG)).String()

I can read file info, but I don’t know how to save file.

image

I try to write like this, but I still can't save it as a picture correctly.
err := ioutil.WriteFile("1.png", []byte(s), 0666)

Do you have any good ideas?

@flexzuu

@flexzuu
Copy link
Contributor Author

flexzuu commented Mar 18, 2021

I will take a look

@electricbubble
Copy link

I make a little attempt.
I'm not good at it. 😔

image

@flexzuu
Copy link
Contributor Author

flexzuu commented Mar 18, 2021

I got it working:

package main

import "C"
import (
	"github.com/progrium/macdriver/cocoa"
	"github.com/progrium/macdriver/core"
	"io/ioutil"
	"unsafe"
)

func main() {
	// get general pasteboard
	gp := cocoa.NSPasteboard_GeneralPasteboard()


	data := gp.Send("dataForType:",  core.NSString_FromString(cocoa.NSPasteboardTypePNG))
	objcBytes := data.Get("bytes")
	length := data.Get("length")
	bs := C.GoBytes(unsafe.Pointer(objcBytes.Pointer()), C.int(length.Uint()))
	err := ioutil.WriteFile("/Users/jonas/Desktop/1.png", bs, 0666)
	if err != nil {
		panic(err)
	}
}

@flexzuu
Copy link
Contributor Author

flexzuu commented Mar 18, 2021

I will need to go now might open PR later with an example or some nicer api for length and bytes

@LinkinStars
Copy link

I tried. It works. Excellent!

@electricbubble
Copy link

I will need to go now might open PR later with an example or some nicer api for length and bytes

Maybe like this

func (pb NSPasteboard) DataForType(t NSPasteboardType) []byte {
	data := pb.Send("dataForType:",  core.NSString_FromString(t))
	objcBytes := data.Get("bytes")
	length := data.Get("length")
	return C.GoBytes(unsafe.Pointer(objcBytes.Pointer()), C.int(length.Uint()))
}

@flexzuu
Copy link
Contributor Author

flexzuu commented Mar 20, 2021

#31 is open and adds a bunch of utility to improve the workflow when using NSPasteboard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants