mirror of
https://github.com/skidoodle/ctx.git
synced 2026-04-28 11:17:42 +02:00
4453856fd1
Create an NSURL from the path and write it to the general pasteboard with writeObjects:. Add nil checks for the created NSURL and remove the legacy NSFilenamesPboardType setPropertyList code.
47 lines
908 B
Go
47 lines
908 B
Go
//go:build darwin
|
|
|
|
package clipboard
|
|
|
|
/*
|
|
#cgo CFLAGS: -x objective-c
|
|
#cgo LDFLAGS: -framework Cocoa
|
|
#include <stdlib.h>
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
int copyFileToPasteboard(char* path) {
|
|
@autoreleasepool {
|
|
NSString *strPath = [NSString stringWithUTF8String:path];
|
|
if (!strPath) return 0;
|
|
|
|
NSURL *url = [NSURL fileURLWithPath:strPath];
|
|
if (!url) return 0;
|
|
|
|
NSPasteboard *pb = [NSPasteboard generalPasteboard];
|
|
[pb clearContents];
|
|
|
|
return [pb writeObjects:@[url]] ? 1 : 0;
|
|
}
|
|
}
|
|
*/
|
|
import "C"
|
|
import (
|
|
"errors"
|
|
"path/filepath"
|
|
"unsafe"
|
|
)
|
|
|
|
func CopyFile(path string) error {
|
|
absPath, err := filepath.Abs(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cPath := C.CString(absPath)
|
|
defer C.free(unsafe.Pointer(cPath))
|
|
|
|
if success := C.copyFileToPasteboard(cPath); success == 0 {
|
|
return errors.New("failed to write to macOS pasteboard")
|
|
}
|
|
return nil
|
|
}
|