Files
ctx/clipboard/darwin.go
T
x af9e0a5372 Add cross-platform clipboard support
Implement CopyFile for darwin, linux, and windows and call it from
main to copy generated output to the clipboard. Enable CGO for builds,
update goreleaser and release workflow to use macOS, and add a
cross-platform test workflow that verifies the clipboard.
2026-01-31 01:53:12 +01:00

44 lines
923 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;
NSPasteboard *pb = [NSPasteboard generalPasteboard];
[pb clearContents];
[pb declareTypes:@[NSFilenamesPboardType] owner:nil];
return [pb setPropertyList:@[strPath] forType:NSFilenamesPboardType] ? 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
}