MCPcopy
hub / github.com/codeaashu/claude-code / copyPngToClipboard

Function copyPngToClipboard

src/utils/screenshotClipboard.ts:46–121  ·  view source on GitHub ↗
(
  pngPath: string,
)

Source from the content-addressed store, hash-verified

44}
45
46async function copyPngToClipboard(
47 pngPath: string,
48): Promise<{ success: boolean; message: string }> {
49 const platform = getPlatform()
50
51 if (platform === 'macos') {
52 // macOS: Use osascript to copy PNG to clipboard
53 // Escape backslashes and double quotes for AppleScript string
54 const escapedPath = pngPath.replace(/\\/g, '\\\\').replace(/"/g, '\\"')
55 const script = `set the clipboard to (read (POSIX file "${escapedPath}") as «class PNGf»)`
56 const result = await execFileNoThrowWithCwd('osascript', ['-e', script], {
57 timeout: 5000,
58 })
59
60 if (result.code === 0) {
61 return { success: true, message: 'Screenshot copied to clipboard' }
62 }
63 return {
64 success: false,
65 message: `Failed to copy to clipboard: ${result.stderr}`,
66 }
67 }
68
69 if (platform === 'linux') {
70 // Linux: Try xclip first, then xsel
71 const xclipResult = await execFileNoThrowWithCwd(
72 'xclip',
73 ['-selection', 'clipboard', '-t', 'image/png', '-i', pngPath],
74 { timeout: 5000 },
75 )
76
77 if (xclipResult.code === 0) {
78 return { success: true, message: 'Screenshot copied to clipboard' }
79 }
80
81 // Try xsel as fallback
82 const xselResult = await execFileNoThrowWithCwd(
83 'xsel',
84 ['--clipboard', '--input', '--type', 'image/png'],
85 { timeout: 5000 },
86 )
87
88 if (xselResult.code === 0) {
89 return { success: true, message: 'Screenshot copied to clipboard' }
90 }
91
92 return {
93 success: false,
94 message:
95 'Failed to copy to clipboard. Please install xclip or xsel: sudo apt install xclip',
96 }
97 }
98
99 if (platform === 'windows') {
100 // Windows: Use PowerShell to copy image to clipboard
101 const psScript = `Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.Clipboard]::SetImage([System.Drawing.Image]::FromFile('${pngPath.replace(/'/g, "''")}'))`
102 const result = await execFileNoThrowWithCwd(
103 'powershell',

Callers 1

copyAnsiToClipboardFunction · 0.85

Calls 2

getPlatformFunction · 0.85
execFileNoThrowWithCwdFunction · 0.85

Tested by

no test coverage detected