long blogs

进一步有进一步惊喜


  • Home
  • Archive
  • Tags
  •  

© 2025 long

Theme Typography by Makito

Proudly published with Hexo

linux自动更换壁纸

Posted at 2020-09-22 linux 

功能描述

每隔一段时间更换linux的壁纸。

golang 实现

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
package main

import (
"encoding/json"
"io/ioutil"
"log"
"math/rand"
"os/exec"
"time"
)
type Config struct {
List []string
Interval int
}
var config = Config{}
func randomPaper(args []string) {
index := rand.Int31n(int32(len(config.List)))
args[3] = config.List[index]
}
func init() {
data,err := ioutil.ReadFile("config.json")
if err != nil {
log.Fatal("read config file err:",err)
}
err = json.Unmarshal(data,&config)
if err != nil{
log.Fatal("unmarshal err:",err)
}
if len(config.List) <= 0 {
log.Fatal("list no exist")
}
if config.Interval < 3 {
log.Fatal("interval is too fast")
}
}
func main() {
args := []string{
"set","org.gnome.desktop.background","picture-uri","",
}
for {
randomPaper(args)
cmd := exec.Command("gsettings",args...)
cmd.Run()
i := time.Second * (time.Duration(config.Interval))
time.Sleep(i)
}
}

配置文件

1
2
3
4
5
6
7
8
{
"list": [
"file:/home/test/Downloads/th2.jpeg",
"file:/home/test/Downloads/th1.jpg",
"file:/home/test/Downloads/th.jpeg"
],
"interval":10
}

Share 

 Previous post: C-内存和类型 Next post: C-AES加解密 

© 2025 long

Theme Typography by Makito

Proudly published with Hexo