// Name: Replace File Path with File Contents // Description: If clipboard text is a file path, replace it with the file's contents. // Author: tayiorbeii // GitHub: tayiorbeii import "@johnlindquist/kit" function stripQuotes(s: string): string { if ((s.startsWith('"') && s.endsWith('"')) || (s.startsWith("'") && s.endsWith("'"))) { return s.slice(1, -1) } return s } function fromFileUrl(url: string): string { // Remove "file://" and decode let decoded = decodeURI(url).replace(/^file:\/\//i, '') // On Windows, file URLs may start with "/C:/..." if (/^\/[a-zA-Z]:\//.test(decoded)) decoded = decoded.slice(1) return decoded } function expandTilde(p: string): string { if (p === '~') return home() if (p.startsWith('~/')) { const rest = p.slice(2) const parts = rest.split(/[\\/]+/).filter(Boolean) return home(...parts) } return p } function normalizePath(raw: string): string { let s = raw.trim() // If multiple lines were copied, use the first line if (s.includes('\n')) s = s.split('\n')[0].trim() s = stripQuotes(s) if (/^file:\/\//i.test(s)) s = fromFileUrl(s) s = expandTilde(s) return s } const current = (await paste())?.trim() || '' if (!current) { exit() } const candidate = normalizePath(current) if (await isFile(candidate)) { try { const content = await readFile(candidate, 'utf8') await copy(content) await toast(`Replaced file path with contents of: ${path.basename(candidate)}`, { autoClose: 2000 }) } catch (err) { await toast(`Failed to read file: ${candidate}`, { autoClose: 3000 }) } } else { // Not a file path; do nothing exit() }