Ariver
2026-06-03 35f7ccfbc29b322306983bf800c120ba816e3e8e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//go:build darwin
 
package input
 
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework AppKit -framework CoreGraphics -framework Carbon -framework ApplicationServices
#include <AppKit/AppKit.h>
#include <CoreGraphics/CoreGraphics.h>
#include <Carbon/Carbon.h>
#include <ApplicationServices/ApplicationServices.h>
#include <stdlib.h>
#include <string.h>
 
static int ensureInputPermission(void) {
    NSDictionary* opts = @{(__bridge NSString*)kAXTrustedCheckOptionPrompt: @YES};
    return AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)opts) ? 1 : 0;
}
 
static void setClipboardText(const char* text) {
    NSPasteboard* pb = [NSPasteboard generalPasteboard];
    [pb clearContents];
    NSString* str = [NSString stringWithUTF8String:text];
    [pb setString:str forType:NSPasteboardTypeString];
}
 
static char* getClipboardText(void) {
    NSPasteboard* pb = [NSPasteboard generalPasteboard];
    NSString* str = [pb stringForType:NSPasteboardTypeString];
    if (!str) return NULL;
    const char* utf8 = [str UTF8String];
    if (!utf8) return NULL;
    return strdup(utf8);
}
 
static void simulateCmdV() {
    CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
 
    CGEventRef cmdDown = CGEventCreateKeyboardEvent(source, (CGKeyCode)kVK_Command, true);
    CGEventSetFlags(cmdDown, kCGEventFlagMaskCommand);
    CGEventPost(kCGHIDEventTap, cmdDown);
 
    CGEventRef vDown = CGEventCreateKeyboardEvent(source, (CGKeyCode)kVK_ANSI_V, true);
    CGEventSetFlags(vDown, kCGEventFlagMaskCommand);
    CGEventPost(kCGHIDEventTap, vDown);
 
    CGEventRef vUp = CGEventCreateKeyboardEvent(source, (CGKeyCode)kVK_ANSI_V, false);
    CGEventSetFlags(vUp, kCGEventFlagMaskCommand);
    CGEventPost(kCGHIDEventTap, vUp);
 
    CGEventRef cmdUp = CGEventCreateKeyboardEvent(source, (CGKeyCode)kVK_Command, false);
    CGEventSetFlags(cmdUp, 0);
    CGEventPost(kCGHIDEventTap, cmdUp);
 
    CFRelease(cmdDown);
    CFRelease(vDown);
    CFRelease(vUp);
    CFRelease(cmdUp);
    CFRelease(source);
}
 
static void typeUnicodeChar(unsigned int codepoint) {
    CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
    UniChar chars[2];
    int len = 0;
 
    if (codepoint <= 0xFFFF) {
        chars[0] = (UniChar)codepoint;
        len = 1;
    } else {
        codepoint -= 0x10000;
        chars[0] = (UniChar)(0xD800 + (codepoint >> 10));
        chars[1] = (UniChar)(0xDC00 + (codepoint & 0x3FF));
        len = 2;
    }
 
    CGEventRef keyDown = CGEventCreateKeyboardEvent(source, 0, true);
    CGEventKeyboardSetUnicodeString(keyDown, len, chars);
    CGEventPost(kCGHIDEventTap, keyDown);
 
    CGEventRef keyUp = CGEventCreateKeyboardEvent(source, 0, false);
    CGEventKeyboardSetUnicodeString(keyUp, len, chars);
    CGEventPost(kCGHIDEventTap, keyUp);
 
    CFRelease(keyDown);
    CFRelease(keyUp);
    CFRelease(source);
}
*/
import "C"
 
import (
    "fmt"
    "time"
    "unsafe"
)
 
type darwinPaster struct{}
 
func newPlatformPaster() Paster {
    return &darwinPaster{}
}
 
func (p *darwinPaster) Paste(text string, keepClipboard bool) error {
    if C.ensureInputPermission() == 0 {
        return fmt.Errorf("accessibility permission is required to paste")
    }
 
    var oldClip *string
    if !keepClipboard {
        cOld := C.getClipboardText()
        if cOld != nil {
            old := C.GoString(cOld)
            oldClip = &old
            C.free(unsafe.Pointer(cOld))
        }
    }
 
    cstr := C.CString(text)
    defer C.free(unsafe.Pointer(cstr))
    C.setClipboardText(cstr)
    time.Sleep(60 * time.Millisecond)
    C.simulateCmdV()
 
    if !keepClipboard && oldClip != nil {
        go func(old string) {
            time.Sleep(600 * time.Millisecond)
            cOld := C.CString(old)
            defer C.free(unsafe.Pointer(cOld))
            C.setClipboardText(cOld)
        }(*oldClip)
    }
 
    return nil
}
 
func (p *darwinPaster) TypeText(text string) error {
    if C.ensureInputPermission() == 0 {
        return fmt.Errorf("accessibility permission is required to type")
    }
    for _, r := range text {
        C.typeUnicodeChar(C.uint(r))
    }
    return nil
}