| |
| |
| |
| |
| |
|
|
| import { exec } from 'node:child_process'; |
| import { promisify } from 'node:util'; |
| import * as fs from 'node:fs/promises'; |
| import * as path from 'node:path'; |
|
|
| const execAsync = promisify(exec); |
|
|
| |
| |
| |
| |
| export async function clipboardHasImage(): Promise<boolean> { |
| if (process.platform !== 'darwin') { |
| return false; |
| } |
|
|
| try { |
| |
| const { stdout } = await execAsync( |
| `osascript -e 'clipboard info' 2>/dev/null | grep -qE "«class PNGf»|TIFF picture|JPEG picture|GIF picture|«class JPEG»|«class TIFF»" && echo "true" || echo "false"`, |
| { shell: '/bin/bash' }, |
| ); |
| return stdout.trim() === 'true'; |
| } catch { |
| return false; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| export async function saveClipboardImage( |
| targetDir?: string, |
| ): Promise<string | null> { |
| if (process.platform !== 'darwin') { |
| return null; |
| } |
|
|
| try { |
| |
| |
| const baseDir = targetDir || process.cwd(); |
| const tempDir = path.join(baseDir, '.gemini-clipboard'); |
| await fs.mkdir(tempDir, { recursive: true }); |
|
|
| |
| const timestamp = new Date().getTime(); |
|
|
| |
| const formats = [ |
| { class: 'PNGf', extension: 'png' }, |
| { class: 'JPEG', extension: 'jpg' }, |
| { class: 'TIFF', extension: 'tiff' }, |
| { class: 'GIFf', extension: 'gif' }, |
| ]; |
|
|
| for (const format of formats) { |
| const tempFilePath = path.join( |
| tempDir, |
| `clipboard-${timestamp}.${format.extension}`, |
| ); |
|
|
| |
| const script = ` |
| try |
| set imageData to the clipboard as «class ${format.class}» |
| set fileRef to open for access POSIX file "${tempFilePath}" with write permission |
| write imageData to fileRef |
| close access fileRef |
| return "success" |
| on error errMsg |
| try |
| close access POSIX file "${tempFilePath}" |
| end try |
| return "error" |
| end try |
| `; |
|
|
| const { stdout } = await execAsync(`osascript -e '${script}'`); |
|
|
| if (stdout.trim() === 'success') { |
| |
| try { |
| const stats = await fs.stat(tempFilePath); |
| if (stats.size > 0) { |
| return tempFilePath; |
| } |
| } catch { |
| |
| } |
| } |
|
|
| |
| try { |
| await fs.unlink(tempFilePath); |
| } catch { |
| |
| } |
| } |
|
|
| |
| return null; |
| } catch (error) { |
| console.error('Error saving clipboard image:', error); |
| return null; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| export async function cleanupOldClipboardImages( |
| targetDir?: string, |
| ): Promise<void> { |
| try { |
| const baseDir = targetDir || process.cwd(); |
| const tempDir = path.join(baseDir, '.gemini-clipboard'); |
| const files = await fs.readdir(tempDir); |
| const oneHourAgo = Date.now() - 60 * 60 * 1000; |
|
|
| for (const file of files) { |
| if ( |
| file.startsWith('clipboard-') && |
| (file.endsWith('.png') || |
| file.endsWith('.jpg') || |
| file.endsWith('.tiff') || |
| file.endsWith('.gif')) |
| ) { |
| const filePath = path.join(tempDir, file); |
| const stats = await fs.stat(filePath); |
| if (stats.mtimeMs < oneHourAgo) { |
| await fs.unlink(filePath); |
| } |
| } |
| } |
| } catch { |
| |
| } |
| } |
|
|