-
Notifications
You must be signed in to change notification settings - Fork 1
/
gnome-screenshot2swappy.go
executable file
·72 lines (65 loc) · 1.39 KB
/
gnome-screenshot2swappy.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//usr/bin/go run $0 $@; exit $?
package main
import (
"io/ioutil"
"math/rand"
"os"
"os/exec"
"strings"
"time"
)
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
func main() {
tmpFile := mkTempName()
if err := exec.Command("gnome-screenshot", "-a", "-f", tmpFile).Run(); err != nil {
panic(err)
}
defer os.Remove(tmpFile)
lastBefore, err := getLastSwappy()
if err != nil {
panic(err)
}
if err := exec.Command("swappy", "-f", tmpFile).Run(); err != nil {
panic(err)
}
lastAfter, err := getLastSwappy()
if err != nil {
panic(err)
}
if lastBefore == lastAfter {
return
}
if err := exec.Command("nautilus", "-s", lastAfter).Start(); err != nil {
panic(err)
}
}
func mkTempName() string {
dir := os.TempDir()
rs := make([]byte, 12)
for i := range rs {
rs[i] = charset[seededRand.Intn(len(charset))]
}
return dir + "/tmp." + string(rs) + ".png"
}
func getLastSwappy() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
base := home + "/Pictures/Screenshots"
infos, err := ioutil.ReadDir(base)
if err != nil {
return "", err
}
fname := ""
for _, f := range infos {
if strings.HasPrefix(f.Name(), "Swappy_") && f.Name() > fname {
fname = f.Name()
}
}
if fname != "" {
fname = base + "/" + fname
}
return fname, nil
}