Compare commits

..

3 Commits

Author SHA1 Message Date
0286dc39eb fix 2021-02-01 18:09:47 +03:00
b265467dc4 clear before paste files 2021-02-01 17:34:13 +03:00
3615552b6d add get clipboard files and image handling 2021-02-01 17:08:39 +03:00
5 changed files with 239 additions and 66 deletions

View File

@ -3,7 +3,7 @@
A tiny library to access the MacOS clipboard (a.k.a. NSPasteboard). A tiny library to access the MacOS clipboard (a.k.a. NSPasteboard).
```go ```go
go get git.wow.st/gmp/clip go get git.wow.st/63l06ri5/clip
``` ```
## API: ## API:
@ -20,4 +20,3 @@ func Set(string) bool
// Get retrieves the string currently on the pasteboard. // Get retrieves the string currently on the pasteboard.
func Get() string func Get() string
``` ```

3
go.mod
View File

@ -1,3 +0,0 @@
module git.wow.st/gmp/clip
go 1.16

45
main.go
View File

@ -1,11 +1,18 @@
package clip package clip
import ( import (
"git.wow.st/gmp/clip/ns" "strings"
"git.wow.st/63l06ri5/clip/ns"
) )
var pb *ns.NSPasteboard var pb *ns.NSPasteboard
const (
uriPrefix = "file://"
uriPrefixLength = len(uriPrefix)
)
func Clear() { func Clear() {
if pb == nil { if pb == nil {
pb = ns.NSPasteboardGeneralPasteboard() pb = ns.NSPasteboardGeneralPasteboard()
@ -32,3 +39,39 @@ func Get() string {
return ret.String() return ret.String()
} }
} }
func PasteFileList(filePaths []string) {
if pb == nil {
pb = ns.NSPasteboardGeneralPasteboard()
}
pb.ClearContents()
pb.PasteFileList(filePaths)
}
func GetFileList() []string {
if pb == nil {
pb = ns.NSPasteboardGeneralPasteboard()
}
ret := pb.GetFileList()
result := make([]string, 0)
if ret.Ptr() == nil {
return result
} else {
filesStr := ret.String()
arr := strings.Split(filesStr, ns.FilesDelimeter)
for _, item := range arr {
if strings.HasPrefix(item, uriPrefix) {
result = append(result, item[uriPrefixLength:])
}
}
return result
}
}
func GetImage(pathToStore string) bool {
if pb == nil {
pb = ns.NSPasteboardGeneralPasteboard()
}
return pb.GetImage(pathToStore)
}

View File

@ -86,18 +86,95 @@ NSPasteboard_inst_GetString(void* o) {
} }
void
NSPasteboard_inst_PasteFileList(void* o, void* string) {
@autoreleasepool {
NSArray *values = [(NSString*)string componentsSeparatedByString:@"\n"];
NSMutableArray *urls = [NSMutableArray arrayWithCapacity:values.count + 1];
for (NSString *value in values) {
id transformedValue = [NSURL fileURLWithPath:value isDirectory:NO];
[urls addObject:transformedValue];
}
NSString* pathPerLine = [values componentsJoinedByString:@"\n"];
NSPasteboard* pasteboard = (NSPasteboard*)o;
[pasteboard clearContents];
[pasteboard writeObjects:urls];
//Now add the pathsPerLine as a string
[pasteboard setString:pathPerLine forType:NSPasteboardTypeString];
}
}
void* _Nullable
NSPasteboard_inst_GetFileList(void* o) {
NSString* _Nullable ret;
@autoreleasepool {
NSArray *classes = [NSArray arrayWithObject:[NSURL class]];
NSDictionary *options = [NSDictionary dictionaryWithObject:
[NSNumber numberWithBool:YES] forKey:NSPasteboardURLReadingFileURLsOnlyKey];
NSArray *fileURLs =
[(NSPasteboard*)o readObjectsForClasses:classes options:options];
NSMutableArray *paths = [NSMutableArray arrayWithCapacity:fileURLs.count];
for (NSURL *value in fileURLs) {
id transformedValue = value.absoluteString;
[paths addObject:transformedValue];
}
ret = [paths componentsJoinedByString:@"\n"];
if (ret != nil && ret != o) { [ret retain]; }
}
return ret;
}
BOOL
NSPasteboard_inst_GetImage(void* o, void* filePath) {
BOOL ret;
@autoreleasepool {
NSData *data = [(NSPasteboard*)o dataForType:NSPasteboardTypeTIFF];
if (data != nil && data != o) {
// NSImage *img = [[NSImage alloc] initWithData:data];
//NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"image.tiff"];
[data writeToFile: (NSString*)filePath atomically: NO];
ret = true;
} else
{
ret = false;
}
}
return ret;
}
*/ */
import "C" import "C"
import ( import (
"unsafe"
"runtime" "runtime"
"strings"
"unsafe"
) )
type Id struct { type Id struct {
ptr unsafe.Pointer ptr unsafe.Pointer
} }
func (o *Id) Ptr() unsafe.Pointer { if o == nil { return nil }; return o.ptr }
const FilesDelimeter = "\n"
func (o *Id) Ptr() unsafe.Pointer {
if o == nil {
return nil
}
return o.ptr
}
type NSObject interface { type NSObject interface {
Ptr() unsafe.Pointer Ptr() unsafe.Pointer
@ -124,8 +201,14 @@ func (c *Char) Free() {
type BOOL C.uchar type BOOL C.uchar
type NSString struct { Id } type NSString struct{ Id }
func (o *NSString) Ptr() unsafe.Pointer { if o == nil { return nil }; return o.ptr }
func (o *NSString) Ptr() unsafe.Pointer {
if o == nil {
return nil
}
return o.ptr
}
func (o *Id) NSString() *NSString { func (o *Id) NSString() *NSString {
return (*NSString)(unsafe.Pointer(o)) return (*NSString)(unsafe.Pointer(o))
} }
@ -144,8 +227,14 @@ func (o *NSString) String() string {
return ret return ret
} }
type NSPasteboard struct { Id } type NSPasteboard struct{ Id }
func (o *NSPasteboard) Ptr() unsafe.Pointer { if o == nil { return nil }; return o.ptr }
func (o *NSPasteboard) Ptr() unsafe.Pointer {
if o == nil {
return nil
}
return o.ptr
}
func (o *Id) NSPasteboard() *NSPasteboard { func (o *Id) NSPasteboard() *NSPasteboard {
return (*NSPasteboard)(unsafe.Pointer(o)) return (*NSPasteboard)(unsafe.Pointer(o))
} }
@ -163,7 +252,9 @@ func (c *Char) String() string {
func NSStringWithUTF8String(nullTerminatedCString *Char) *NSString { func NSStringWithUTF8String(nullTerminatedCString *Char) *NSString {
ret := &NSString{} ret := &NSString{}
ret.ptr = unsafe.Pointer(C.NSString_StringWithUTF8String(unsafe.Pointer(nullTerminatedCString))) ret.ptr = unsafe.Pointer(C.NSString_StringWithUTF8String(unsafe.Pointer(nullTerminatedCString)))
if ret.ptr == nil { return ret } if ret.ptr == nil {
return ret
}
runtime.SetFinalizer(ret, func(o *NSString) { runtime.SetFinalizer(ret, func(o *NSString) {
o.Release() o.Release()
}) })
@ -180,7 +271,9 @@ func NSStringWithGoString(string string) *NSString {
func NSPasteboardGeneralPasteboard() *NSPasteboard { func NSPasteboardGeneralPasteboard() *NSPasteboard {
ret := &NSPasteboard{} ret := &NSPasteboard{}
ret.ptr = unsafe.Pointer(C.NSPasteboard_GeneralPasteboard()) ret.ptr = unsafe.Pointer(C.NSPasteboard_GeneralPasteboard())
if ret.ptr == nil { return ret } if ret.ptr == nil {
return ret
}
return ret return ret
} }
@ -191,7 +284,7 @@ func (o *NSPasteboard) ClearContents() {
func (o *NSPasteboard) SetString(s string) bool { func (o *NSPasteboard) SetString(s string) bool {
string := NSStringWithGoString(s) string := NSStringWithGoString(s)
ret := (C.NSPasteboard_inst_SetString(o.Ptr(), string.Ptr())) != false ret := (C.NSPasteboard_inst_SetString(o.Ptr(), string.Ptr())) != 0
runtime.KeepAlive(o) runtime.KeepAlive(o)
runtime.KeepAlive(string) runtime.KeepAlive(string)
return ret return ret
@ -200,8 +293,14 @@ func (o *NSPasteboard) SetString(s string) bool {
func (o *NSPasteboard) GetString() *NSString { func (o *NSPasteboard) GetString() *NSString {
ret := &NSString{} ret := &NSString{}
ret.ptr = unsafe.Pointer(C.NSPasteboard_inst_GetString(o.Ptr())) ret.ptr = unsafe.Pointer(C.NSPasteboard_inst_GetString(o.Ptr()))
if ret.ptr == nil { runtime.KeepAlive(o); return ret } if ret.ptr == nil {
if ret.ptr == o.ptr { runtime.KeepAlive(o); return (*NSString)(unsafe.Pointer(o)) } runtime.KeepAlive(o)
return ret
}
if ret.ptr == o.ptr {
runtime.KeepAlive(o)
return (*NSString)(unsafe.Pointer(o))
}
runtime.SetFinalizer(ret, func(o *NSString) { runtime.SetFinalizer(ret, func(o *NSString) {
o.Release() o.Release()
}) })
@ -209,3 +308,38 @@ func (o *NSPasteboard) GetString() *NSString {
return ret return ret
} }
func (o *NSPasteboard) PasteFileList(values []string) {
inString := strings.Join(values, FilesDelimeter)
str := NSStringWithGoString(inString)
C.NSPasteboard_inst_PasteFileList(o.Ptr(), str.Ptr())
runtime.KeepAlive(o)
runtime.KeepAlive(str)
return
}
func (o *NSPasteboard) GetFileList() *NSString {
ret := &NSString{}
ret.ptr = unsafe.Pointer(C.NSPasteboard_inst_GetFileList(o.Ptr()))
if ret.ptr == nil {
runtime.KeepAlive(o)
return ret
}
if ret.ptr == o.ptr {
runtime.KeepAlive(o)
return (*NSString)(unsafe.Pointer(o))
}
runtime.SetFinalizer(ret, func(o *NSString) {
o.Release()
})
runtime.KeepAlive(o)
return ret
}
func (o *NSPasteboard) GetImage(filePath string) bool {
string := NSStringWithGoString(filePath)
ret := (C.NSPasteboard_inst_GetImage(o.Ptr(), string.Ptr())) != 0
runtime.KeepAlive(o)
runtime.KeepAlive(string)
return ret
}

View File

@ -1,7 +1,7 @@
# binding generator for git.wow.st/gmp/nswrap # binding generator for git.wow.st/gmp/nswrap
# original binding is in ns/main.go- and is not used. # original binding is in ns/main.go- and is not used.
inputfiles: inputfiles:
- /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSPasteboard.h - /System/Library/Frameworks/AppKit.framework/Headers/NSPasteboard.h
classes: classes:
- NSPasteboard - NSPasteboard
- NSString - NSString