-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
49 lines (40 loc) · 1.03 KB
/
main.go
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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/atotto/clipboard"
"github.com/otiai10/gosseract/v2"
)
func main() {
// Define the temporary screenshot file
tmpDir := os.TempDir()
screenshotFile := filepath.Join(tmpDir, fmt.Sprintf("screenshot_%d.png", time.Now().Unix()))
// Command to capture selected area
cmd := exec.Command("scrot", "-s", screenshotFile)
err := cmd.Run()
if err != nil {
log.Fatalf("Failed to take screenshot: %v", err)
}
// Check if the screenshot file exists
if _, err := os.Stat(screenshotFile); os.IsNotExist(err) {
log.Fatalf("Screenshot file does not exist: %v", err)
}
// Perform OCR
client := gosseract.NewClient()
defer client.Close()
client.SetImage(screenshotFile)
text, err := client.Text()
if err != nil {
log.Fatalf("Failed to perform OCR: %v", err)
}
// Copy text to clipboard
err = clipboard.WriteAll(text)
if err != nil {
log.Fatalf("Failed to copy text to clipboard: %v", err)
}
fmt.Println("Text copied to clipboard successfully.")
}