// Name: Screenshot OCR Note
// Description: Take a screenshot, extract text with OCR, edit and save as note
// Author: arvindcr4
import "@johnlindquist/kit"
import Tesseract from 'tesseract.js'
// Take screenshot
const buffer = await screenshot()
// Save screenshot temporarily for display
const tmpImagePath = tmpPath('screenshot-ocr.png')
await writeFile(tmpImagePath, buffer)
// Extract text using OCR
const extractedText = await div({
html: md(`
## Extracting text from screenshot...
<img src="file://${tmpImagePath}" class="max-w-full max-h-96 object-contain" />
`),
onInit: async () => {
try {
const { data } = await Tesseract.recognize(buffer, 'eng', {
logger: (m) => console.log(m),
})
submit(data.text)
} catch (error) {
console.error('OCR Error:', error)
submit('Error extracting text from screenshot')
}
},
})
// Edit the extracted text
const editedText = await editor({
value: extractedText,
language: 'markdown',
placeholder: 'Edit the extracted text...',
hint: 'Make any corrections to the OCR text, then save',
})
// Create notes directory if it doesn't exist
const notesDir = kenvPath('notes')
await ensureDir(notesDir)
// Generate filename with timestamp
const timestamp = formatDate(new Date(), 'yyyy-MM-dd-HH-mm-ss')
const filename = `screenshot-note-${timestamp}.md`
const notePath = path.join(notesDir, filename)
// Save the note
await writeFile(notePath, editedText)
// Show confirmation and reveal file
await div(md(`
# Note Saved! 📝
**File:** ${filename}
**Location:** ${notePath}
The extracted and edited text has been saved to your notes.
`))
// Reveal the saved note in finder
await revealFile(notePath)